The heredoc structure and nowdoc structure of the string in PHP, heredocnowdoc
Recently, I am maintaining an old PHP website without the separation of the front and back ends. The code looks very hard and the HTML output of the above section is large. At the time of reading, we found that many places use symbols such as <to mark strings. It seems that when learning PHP, We only talked about two double quotation marks to represent strings. Today, I accidentally saw in the book that the "<" character string structure is heredoc and nowdoc.
Echo <EOT
In general, if a string contains quotation marks, we will use "\" for escape. In PHP, single quotation marks and double quotation marks can both represent strings, therefore, you can use single quotes to indicate the start and end of a string.
Echo 'the double quotation marks here do not need to be escaped ';
In HTML, tag attributes are also represented by xxx = "yyy.
<divid="div-id"class="div-class"></div>
When outputting a large HTML segment, it is too much work to escape all double quotes, and it is not very convenient for maintenance. errors that are not aligned with quotation marks often occur. In general, we use single quotes to indicate a string with double quotation marks in this large segment.
However, in HTML, single quotes are also allowed to represent attributes. If a single double quotation mark is mixed in the output HTML, the traditional single double quotation mark string is troublesome. Therefore, the heredoc mode is used to represent the string.
echo<<<EOT <div id="div-id"class="div-class">$content</div>EOT;
The heredoc mode defines an end tag EOT for another string. The single double quotation marks between <EOT "and" EOT "are not used as the start and end tags of the string. This is useful for formatting output content.
Heredoc has the following features:
1. Start and end tags use the same string, which is usually written with uppercase letters.
2. do not contain spaces or extra characters after the start mark.
3. The ending mark must be written at the top, with no indentation or space, and a semicolon at the end of the ending mark.
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 concatenated using the connector "." or.
In PHP5.3.0 and later versions, the nowdoc structure is added, which is the same as heredoc. The difference is that nowdoc is similar to a single quotation mark and does not parse the variables between the start mark and the end mark, this structure is suitable for embedding PHP code or other large text without escaping special characters.
In the same example, the nowdoc structure can be used as follows:
echo<<<'EOT' <div id="div-id"class="div-class">$content</div>EOT;
The only difference from heredoc structure usage is to start marking and enclose it with quotation marks. Here, $ content is output as is instead of parsed into variable values.
When using the heredoc and nowdoc structures, the following errors are often reported:
Parse error: syntax error, unexpected T_SL in php
Generally, this problem is caused by space after the separator. For example, if there is space behind the EOT above, this error will be reported. There is also a small trick to deal with whether there are spaces, that is, to use the code prompt function of the editor, take the notepad ++ I used as an example.
In the normal code, the entire heredoc structure string is gray.
Self-moving technology blog: http://www.cc-lab.cn/heredocandnowdoc/