Home >Backend Development >PHP Tutorial >Things to note when using nowdoc and heredoc in PHP_PHP Tutorial

Things to note when using nowdoc and heredoc in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:19833browse

Finally, I discovered the problem. It turns out that this type of document must be close to the first position of the line at the end, which means that there cannot be a space before EOF. For the sake of code indentation, this problem happened.

Copy code The code is as follows:

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;//EOD must be close to the front, without spaces, and do not miss the ending semicolon;


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 to use {$name->change()} for PHP variables in heredoc Enclosed in curly brackets, this can avoid certain ambiguities. If you want to output it as it is, you can use the legendary escape character. The escape character itself can be output using the escape character, that is, this representation method. Braces and the like are all Output needs to be escaped.
In order to ensure that it is usable, it is recommended to use heredoc syntax, which also has escapes. Because the nowdoc syntax was introduced in PHP5.3, many cloud hosting environments may not support it, causing problems.
Finally, 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.

Simply put:
1. Heredoc is dynamic and nowdoc is static
2. Heredoc is similar to multi-line double quotes; newdoc is similar to multi-line single quotes
3. Heredoc is a specialized processing A general processing solution for large strings, and 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/744336.htmlTechArticleFinally, I discovered the problem. It turns out that at the end of this type of document, it must be close to the first line of the line, that is It is said that there cannot be a space before EOF, but for the sake of code indentation, this problem arises...
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
Previous article:Example of php getting domain name included in google_PHP tutorialNext article:Example of php getting domain name included in google_PHP tutorial

Related articles

See more