What is the difference between nowdoc and heredoc in php?

怪我咯
Release: 2023-03-13 16:14:01
Original
1609 people have browsed it

The Heredoc structure is like a double-quoted string without the double quotes, which means that the single quotes are not escaped in the heredoc structure. Variables in its structure will be replaced, but be careful when containing complex variables in a heredoc structure. It is more useful when formatting output content. Specifically, it has the following characteristics:

1. The start tag and the end tag use the same string, usually written in capital letters.

2. No spaces or extra characters can appear after the opening tag.

3. The closing tag must be written at the beginning, without indentation or spaces, and there must be a semicolon at the end of the closing tag.

4. Variables between the start tag and the end tag can be parsed normally, but functions cannot. In heredoc, variables do not need to be spliced ​​with the connector . or .


The nowdoc structure is added in PHP 5.3.0 and later versions. Its usage is the same as heredoc. The difference is that the Nowdoc structure is similar to a single quoted string. No parsing operations are performed in nowdoc. This structure is suitable for embedding PHP code or other large pieces of text without escaping the special characters. Similar to the SGML structure, which is used to declare large sections of text that do not need to be parsed, the nowdoc structure also has the same characteristics.

A nowdoc structure also uses the same tags <<< as the heredocs structure, but the following identifier must be enclosed in single quotes, that is, <<<'EOT '.


heredoc uses the <<< EOT identifier, while nowdoc uses the <<< 'EOT' identifier, where nowdoc is a new technology introduced in PHP5.3. It 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
PHP variables in heredoc It is recommended to use {$name->change()} enclosed in curly brackets, which can avoid certain ambiguities. If you want to output the original , you can use the legendary escape character \, and the escape character itself can be used Escape characters for output, that is, \ this representation method, braces and the like need to be escaped for output.
In order to ensure that it is usable, it is recommended to use the heredoc syntax, which also has escapes. Because the nowdoc syntax was introduced in PHP5.3, many cloud hosting environments may not support it, resulting in a breakdown.
Finally, heredoc was introduced from PHP 4.0, and 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, nowdoc is static
2. Heredoc is similar to multi-line double quotes, newdoc is similar to multi-line single quotes. Quotation marks
3. heredoc is a general processing solution that specializes in processing large strings, while nowdoc is a "highly efficient" static version implemented by PHP to make up for the efficiency problem of dynamic implementation of "heredoc"

The above is the detailed content of What is the difference between nowdoc and heredoc in php?. 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