What is heredoc and nowdoc in PHP

The heredoc structure is similar to double-quoted strings, and the nowdoc structure is similar to single-quoted strings. The nowdoc structure is very similar to the heredoc structure, but nowdoc does not perform parsing operations. This structure works well for PHP code and other large chunks of text that don't need escaping. Similar to SGML's <![CDATA[ ]]> structure, which is used to declare large chunks of unparsed text, the nowdoc structure has the same characteristics.
A nowdoc structure also uses the same tag <<< as a heredocs structure, but the following tags are enclosed in single quotes, as in <<<'EOT'. All the rules for the heredocs structure also apply to the nowdoc structure, especially the rules for closing markers.
Take a look at the example heredoc below:

$name="JBilder";$age="18";$title="JBilder_9986";
echo "这里是heredoc的使用:<br />";
echo <<<JBilderPHP
How to solve the session coverage when customizing the session? ? ? My name is: { $name }; Age is: { $age }; Title is: { $title }. 
JBilderPHP;

The result obtained is:


Take a look at the nowdoc example below:

1  $name ="JBilder"; $age ="18"; $title ="JBilder_9986" ;
 2  echo "Here is the use of heredoc: <br />" ;
 3  echo <<< JBilderPHP
 4 how to solve the custom session When is the session overwritten? ? ? My name is: { $name }; Age is: { $age }; Title is: { $title }.
 5 JBilderPHP;
View Code

The result obtained is:

Note that curly braces are used for heredoc variables, if you write it directly like the following:

1  echo <<<'JBilderPHP'
 2 How to solve the session coverage when customizing the session? ? ? My name is: $name ; Age is: $age ; Title is: $title .
 3 JBilderPHP;
View Code

The following informative error will appear:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325353902&siteId=291194637