The editor will help you learn the delimiter. Is there any difference between nowdoc and heredoc?

醉折花枝作酒筹
Release: 2023-03-11 21:00:01
Original
2289 people have browsed it

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 = <<<&#39;EOD&#39;
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
/* 含有变量的更复杂的示例 */
class foo
{
    public $foo;
    public $bar;
    function foo()
    {
        $this->foo = &#39;Foo&#39;;
        $this->bar = array(&#39;Bar1&#39;, &#39;Bar2&#39;, &#39;Bar3&#39;);
    }
}
$foo = new foo();
$name = &#39;MyName&#39;;
echo <<<&#39;EOT&#39;
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital &#39;A&#39;: x41
EOT;
?>
Copy after login

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;
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template