Home  >  Article  >  Backend Development  >  The editor will help you learn the delimiter. Is there any difference between nowdoc and heredoc?

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

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-08-02 10:58:252232browse

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;
?>

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 f8986e891cd678c68be544a3c986907dchange()}Enclosed in curly brackets to avoid some Ambiguity. If you want to output it as it is, you can use the legendary escape character \. 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!

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