


Things to note when using nowdoc and heredoc in PHP_PHP Tutorial
Jul 13, 2016 am 10:35 AM
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.
$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"

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
