Home  >  Article  >  Backend Development  >  Introduction to heredoc and nowdoc in php, phperedocnowdoc_PHP tutorial

Introduction to heredoc and nowdoc in php, phperedocnowdoc_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:55837browse

Introduction to heredoc and nowdoc in php, phpredocnowdoc

Heredoc technology is generally not described in detail in formal PHP documents and technical books. It is only mentioned that it is a Perl-style string output technology. However, some current forum programs and some article systems cleverly use heredoc technology to partially realize the quasi-separation of interface and code. The phpwind template is a typical example.

1. Start with the <<

2. Variables located between the start tag and the end tag can be parsed normally, but functions cannot. In heredoc, variables do not need to be spliced ​​with connectors. or,, as follows:

Copy code The code is 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 outputting documents containing a large number of HTML syntax. For example: the function outputhtml() should output the HTML home page. There are two ways to write it. Obviously the second way of writing is simpler and easier to read.

Copy code The code is as follows:

function outputhtml(){
echo "";
echo "Homepage";
echo "Homepage content";
echo ";
}
function outputhtml()
{
echo <<
Homepage
Homepage content

EOT;
}
outputhtml();

The $ variable will be automatically replaced in heredoc, and the command and input will be put together for convenience

Attachment: The difference between heredoc and nowdoc

heredoc uses the <<< EOT identifier, while nowdoc uses the <<< 'EOT' identifier. Nowdoc is a new technology introduced in PHP5.3, which contains the syntax of heredoc. , but the content will never be escaped or interpreted in any way. The content is what it is, and PHP-related content will not be parsed
It is recommended that PHP variables in heredoc be enclosed in {$name->change()} braces, which can avoid certain ambiguities. If you want to output them as they are, you can use the legendary escape characters, and the escape characters themselves can be escaped. Characters are output, that is, this representation method, and braces and the like need to be escaped and output.
In order to ensure that it is usable, it is recommended to use the heredoc syntax, which also has escaping. Because the nowdoc syntax was introduced in PHP5.3, many cloud hosting environments may not support it, causing problems.
Finally, I would like to emphasize that heredoc was introduced from PHP 4.0, while nowdoc syntax requires version 5.3. Because heredoc includes the functions of nowdoc, I personally suggest that it is better to use heredoc.

In short:

1. heredoc is dynamic nowdoc is static
2. heredoc is similar to multi-line double quotes and newdoc is similar to multi-line single quotes
3. heredoc is a general processing solution that specializes in processing large segments of strings, while nowdoc is a "highly efficient" static version implemented by PHP to make up for the efficiency problem of dynamic implementation of "heredoc"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932475.htmlTechArticleIntroduction to heredoc and nowdoc in php, phpheredocnowdoc Heredoc technology is generally not detailed in formal PHP documents and technical books Narrated, just mentioned that this is a Perl style string...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn