Introduction to Heredoc and Nowdoc in PHP _php tips

Source: Internet
Author: User
Tags opening and closing tags cloud hosting

Heredoc technology, in regular PHP documents and technical books are not generally described in detail, but mentioned that this is a Perl-style string output technology. But now some of the Forum program, and some of the article system, are clever use of heredoc technology, to part of the implementation of the interface and code of the quasi-separation, phpwind template is a typical example.

1. Start with <<<end start tag, end With end tag, end tag must be write, no indents and spaces, and semicolon at end of tag. The start tag is the same as the start tag, such as the commonly used uppercase EoT, EOD, EOF, but not just those, as long as the start and end tags do not appear in the body.

2. Variables located between the opening and closing tags can be parsed normally, but the function may not. In Heredoc, a variable does not require a connector. Or, to splice, as follows:

Copy Code code as follows:

$v = 2;
$a = <<<eof
"ABC" $v
"123"
EOF;
echo $a; The result is output along with double quotes: "ABC" 2 "123"

3.heredoc is often used when output contains a large number of HTML syntax D documents. For example: function outputhtml () to output the HTML homepage. There are two ways to do this. It is obvious that the second formulation is simpler and easier to read.

Copy Code code as follows:

function outputhtml () {
echo "echo "echo "<body> homepage content </body>";
echo "}
function outputhtml ()
{
Echo <<<eot
<body> Homepage Content </body>
EOT;
}
Outputhtml ();

The $ variable is automatically replaced in the Heredoc, and the command and input are placed together to facilitate

Attachment: The difference between Heredoc and Nowdoc

Heredoc uses <<< EOT identifiers, and Nowdoc uses identifiers such as <<< ' EOT '. Nowdoc is the introduction of PHP5.3 new technology, it contains the Heredoc syntax, but the content will not be any escape and interpretation, what content is what content, will not parse PHP-related content
The PHP variables in Heredoc are recommended to be enclosed in {$name->change ()} braces to avoid ambiguity, and if you want to output as is, you can use the legendary escape character \, the escape character itself can be exported using the escape character, that is, the representation method, Braces like these all need to escape the output.
In order to ensure that it is available, it is recommended that the syntax of Heredoc be used with escape, because the Nowdoc syntax introduced by PHP5.3, many cloud hosting environments may not support the cause of the leftovers.
Finally, the Heredoc is introduced from the PHP4.0, and Nowdoc syntax requires 5.3 version, because the Heredoc contains Nowdoc function, so personal advice or use heredoc better.

In simple terms:

1, Heredoc is the dynamic nowdoc is static
2. Heredoc double quotes similar to multiple lines Newdoc single quotes similar to multiple lines
3, Heredoc is a special processing of large segments of the string, and Nowdoc is PHP in order to make up for the dynamic implementation of "heredoc" efficiency of the implementation of the "efficient" static version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.