Heredoc technology, in regular PHP documents and technical books are not generally described in detail, but mentioned that this is a Perl-style string output technology. But now some of the Forum program, and some of the article system, are clever use of heredoc technology, to part of the implementation of the interface and code of the quasi-separation, phpwind template is a typical example.
As follows:
Copy Code code as follows:
<?php
$name = ' Shallow Water tour ';
Print <<<eot
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>untitled document</title>
<body>
<!--12321-->
Hello, $name!
</body>
EOT;
?>
1. To
<<<endThe start tag begins to
EndEnd Tag End,The end tag must be written in a header, with no indentation and no spaces, and a semicolon at the end of the closing tag。 The start tag is the same as the start tag, such as EOT, EOD, EOF, which is commonly used in uppercase, but not limited to those, as long as the guaranteestart and end tags do not appear in the body。
2. Variables located between the opening and closing tags can be parsed normally, but the function may not . In Heredoc, a variable does not require a connector. Or, to splice, as follows:
Copy Code code as follows:
$v = 2;
$a = <<<eof
"ABC" $v
"123"
EOF;
echo $a; The result is output along with double quotes: "ABC" 2 "123"
3.heredoc is often used when output contains a large number of HTML syntax D documents. For example: function outputhtml () to output the HTML homepage. There are two ways to do this. It is obvious that the second formulation is simpler and easier to read.
Copy Code code as follows:
function outputhtml () {
echo "
echo "echo "<body> homepage content </body>";
echo "
}
function outputhtml ()
{
Echo <<<eot
<body> Homepage Content </body>
EOT;
}
Outputhtml ();