Home >Backend Development >PHP Tutorial >How to use heredoc in php_PHP tutorial

How to use heredoc in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:42:50967browse

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 <<




Untitled Document



Hello,$name!

EOT;
?>

1. Start with << start tag, end with End end tag, end tag must Write at the top, without indentation or spaces, and there must be a semicolon at the end of the closing tag. The start tag is the same as the start tag. For example, it is commonly represented by uppercase EOT, EOD, and EOF, but it is not limited to those few. Just make sure that the start tag and end tag do not appear in the text .

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= << "abc"$v
"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 "Homepage";
echo "Homepage content";
echo ";
}

function outputhtml()
{
echo <<
Homepage
Homepage content

EOT;
}

outputhtml();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486030.htmlTechArticleHeredoc technology is generally not described in detail in regular PHP documents and technical books. It is just mentioned that this is a A Perl-style string output technique. But some of the current forum programs...
Statement:
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