Analyze the use of heredoc in php_PHP tutorial

WBOY
Release: 2016-07-21 15:05:52
Original
753 people have browsed it

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:

Copy code The code 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 be written at the beginning, There can be no 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 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:

Copy the code The code is as follows:

$v=2;
$a= <<"abc"$v
"123"
EOF;
echo $a; //Result with double quotes Output together: "abc"2 "123"

3. heredoc is often used when outputting documents containing a large number of HTML syntax documents. 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.
Copy code The code is as follows:

function outputhtml(){
echo "";
echo "Homepage";
echo "Homepage content";
echo ";
}

function outputhtml()
{
echo <<
Homepage Home page content

EOT;
}
outputhtml();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327655.htmlTechArticleHeredoc technology is generally not described in detail in regular PHP documents and technical books, but is just mentioned. A Perl-style string output technique. But some of the current forum programs...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template