In the previous article, we learned about EOF in PHP. If you need it, please read "What is heredoc? What can it do for PHP?" 》. This time we will introduce to you the difference between nowdoc and heredoc. You can refer to it if necessary.
In the previous section, the editor introduced heredoc to everyone, but it seems that nowdoc was not introduced. But this is not the case. The editor also introduced nowdoc to everyone, but the editor did not tell everyone the term. But let's take a look at nowdoc.
First let’s look at a little chestnut.
<?php $str = <<<'EOD' Example of string spanning multiple lines using nowdoc syntax. EOD; /* 含有变量的更复杂的示例 */ class foo { public $foo; public $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<'EOT' My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': x41 EOT; ?>
It’s not obvious in this way. Let’s take the example from the previous article and compare it.
<?php $name="runoob"; $a= <<<EOF "abc"$name "123" EOF; // 结束需要独立一行且前后不能空格 echo $a; ?>
Let’s look at these two examples and find that the biggest difference is the word EOF. In nowdoc, it requires single quotes, but in heredoc, it does not need to be quoted. of.
This is the difference we intuitively feel. Let’s talk about the difference between nowdoc and heredoc in detail.
heredoc uses the <<< EOT identifier, while nowdoc uses the <<< 'EOT' identifier. Among them, nowdoc is a new technology introduced in php5.3. It contains the syntax of herdoc, but the content is never escaped or interpreted. What is the content, it will not parse content related to PHP
It is recommended to use PHP variables in heredoc{$name->change()}Enclosed in curly brackets to avoid some Ambiguity. If you want to output it as it is, you can use the legendary escape character <code>\
. The escape character itself can be output using escape characters. That is to say, this representation method, braces, etc. all need to be escaped for output. .
To ensure usability, it is recommended to use herdoc syntax with escape. Since nowdoc syntax was introduced in PHP5.3, many cloud hosting environments may not support it, making it impossible to use it.
Finally, I want to emphasize that herdoc was introduced from php4.0, while nowdoc syntax requires version 5.3. Because herdeoc contains functions of nowdoc, I suggest using herdeoc is better.
Simply put:
1. Heredoc is dynamic, while nowdoc is static
2. Herdoc is similar to multi-line double quotes, and NewDoc is similar to multi-line single quotes. Quotation marks
3. heredoc is a general solution for processing large strings, and nowdoc is an "efficient" static version implemented by PHP, which makes up for the efficiency of the dynamic implementation of "Heredoc".
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of The editor will help you learn the delimiter. Is there any difference between nowdoc and heredoc?. For more information, please follow other related articles on the PHP Chinese website!