This article mainly introduces HEREDOC in php, and describes the differences between nowdoc and heredoc in detail. we recommend this article for your reference. Heredoc technology is generally not detailed in regular PHP documents and technical books. it only mentions that it is a Perl-style string output technology. However, some forum programs and some article systems use heredoc to partially implement the quasi-separation of interfaces and codes. phpwind template is a typical example.
1. use <
2. 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 with connectors. or, as shown below:
The code is as follows:
$ V = 2;
$ A = < "Abc" $ v
"123"
EOF;
Echo $ a; // The result is output together with double quotation marks: "abc" 2 "123"
3. heredoc is often used to output a large number of HTML syntax d documents. For example, the outputhtml () function outputs the HTML homepage. There can be two writing methods. Obviously, the second method is relatively simple and easy to read.
The code is as follows:
Function outputhtml (){
Echo"";
Echo"Home Page";
Echo"Homepage content";
Echo";
}
Function outputhtml ()
{
Echo <
Home Page
Homepage content
EOT;
}
Outputhtml ();
In heredoc, the $ variable is automatically replaced, and the command and input are placed in one segment for convenience.
Appendix: differences between heredoc and nowdoc
Heredoc uses the <EOT identifier, while nowdoc uses the <'eot' identifier. nowdoc is a new technology introduced in PHP5.3 and contains the heredoc syntax, however, the content will never be converted or interpreted. what is the content and PHP-related content will not be parsed.
We recommend that you enclose the PHP variable {$ name-> change ()} in brackets in heredoc to avoid ambiguity. if you want to output the variable as is, you can use the legendary escape character \, escape characters can be output using escape characters, that is, the \ representation method. escape output is required for all the braces.
To ensure availability, we recommend that you use the heredoc syntax, which is also escaped. because the nowdoc syntax introduced by PHP5.3, it is very likely that it is not supported by the multi-cloud hosting environment, resulting in a break.
At last, heredoc was introduced from PHP4.0, while nowdoc requires version 5.3. because heredoc contains the nowdoc function, it is recommended that heredoc be used.
To put it simply:
1. heredoc is dynamic and nowdoc is static.
2. heredoc is similar to the double quotation marks of multiple rows. newdoc is similar to the single quotation marks of multiple rows.
3. heredoc is a general solution dedicated to processing large string segments, while nowdoc is a static version of php that achieves "high efficiency" to make up for the dynamic implementation of the "heredoc" efficiency problem.