Introduction to Heredoc and Nowdoc in PHP, Phpheredocnowdoc
Heredoc technology, usually not detailed in regular PHP documentation and technical books, just mentions that this is a Perl-style string output technique. But now some of the forum procedures, and some of the article system, are clever use of heredoc technology, to partially realize the interface and code of the quasi-separation, phpwind template is a typical example.
1. <<
2. Variables located between the opening and closing tags can be parsed normally, but the function is not. In Heredoc, a variable does not need a connector. Or, to splice, as follows:
Copy the Code code as follows:
$v = 2;
$a = <<
"ABC" $v
"123"
EOF;
echo $a; The result is output together with double quotes: "ABC" 2 "123"
3.heredoc is often used when the output contains a large number of HTML syntax D documents. For example: function outputhtml () to output the HTML home page. There are two ways to do this. Obviously the second is simpler and easier to read.
Copy the Code code as follows:
function outputhtml () {
echo "";
echo "Home";
echo "Homepage content";
echo ";
}
function outputhtml ()
{
Echo <<
Home
Home Page Content
EOT;
}
Outputhtml ();
The $ variable is automatically replaced in Heredoc and the command and input are placed in a piece to facilitate
Attached: The difference between Heredoc and Nowdoc
Heredoc uses <<< EOT identifiers, and Nowdoc uses identifiers such as <<< ' EOT ', Nowdoc is a new technology introduced by PHP5.3, it contains heredoc syntax, but the content of which is absolutely no escape and interpretation, what content is what content, will not parse PHP related content
PHP variables in Heredoc are recommended to be enclosed in {$name->change ()} braces, which avoids some ambiguity, and if you want to use the legendary escape character \, the escape character itself can be output using an escape character, that is, the representation method, These need to be escaped out of the curly braces.
To ensure that it is indeed available, it is recommended to use Heredoc syntax, which itself is also escaped, because PHP5.3 introduced the Nowdoc syntax, many cloud hosting bad environment will probably not support the cause of the rest of the dish.
Finally, Heredoc is introduced from PHP4.0, and Nowdoc syntax requires 5.3, because Heredoc contains nowdoc functionality, so it is better to use Heredoc for personal advice.
In simple terms:
1, Heredoc is dynamic nowdoc is static
2, heredoc similar to multiple lines of double quotes newdoc similar to multiple lines of single quotation marks
3, Heredoc is a kind of general processing scheme that deals with large-segment string, and Nowdoc is the "high-efficiency" static version of PHP to make up the efficiency problem of "heredoc" in the dynamic implementation.
http://www.bkjia.com/PHPjc/932475.html www.bkjia.com true http://www.bkjia.com/PHPjc/932475.html techarticle PHP Heredoc and nowdoc Introduction, Phpheredocnowdoc Heredoc technology, in regular PHP documents and technical books are not generally described in detail, just mentioned this is a Perl-style string ...