Home >Backend Development >PHP Tutorial >How to use heredoc in php_PHP tutorial
Heredoc technology is generally not described in detail in formal PHP documents and technical books. It is only mentioned that it is a Perl-style string output technology. However, some current forum programs and some article systems cleverly use heredoc technology to partially realize the quasi-separation of interface and code. The phpwind template is a typical example.
is as follows:
$name = shallow water swimming;
print <<
Hello,$name!
EOT;
?>
1. Start with <<
2. Variables located 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 connectors. or,, as follows:
$v=2;
$a= <<
"123"
EOF;
echo $a; //The result is output together with double quotes: "abc"2 "123"
3. heredoc is often used when outputting documents containing a large number of HTML syntax. For example: the function outputhtml() should output the HTML home page. There are two ways to write it. Obviously the second way of writing is simpler and easier to read.
function outputhtml(){
echo "";
echo "
function outputhtml()
{
echo <<
outputhtml();