Detailed explanation of heredoc method (EOF) in php

WBOY
Release: 2016-07-25 08:58:22
Original
943 people have browsed it
This article introduces the usage of heredoc in PHP, which can output long paragraphs of document content. Some friends may refer to it.

Heredoc technology, in most programming examples, generally only a Perl-style string output technology is introduced. Nowadays, many PHP programs cleverly use heredoc technology to partially realize the quasi-separation of interface and code. Among them, phpwind template is a typical application example.

For example:

<?php 
$name = '脚本 学堂';
print <<<EOT
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>bbs.it-home.org</title> 
</head> 
<body> 
<!--12321--> 
Hello,$name! 
</body> 
</html>
EOT;
?>
Copy after login

Instructions: 1. Start with the <<

<?php
$v=2;
$a= <<<EOF
"abc"$v
"123"
EOF;
echo $a; //结果连同双引号一起输出:"abc"2 "123"
?>
Copy after login

3. Heredoc is commonly used when outputting documents containing a large number of HTML syntax. For example: the function outputhtml() should output the HTML homepage.

can be written in two ways. Obviously the second way of writing is simpler and easier to read.

<?php
function outputhtml(){
echo "<html>";
echo "<head><title>脚本 学堂 主页</title></head>"; 
echo "<body>主页内容</body>";
echo "</html>;
}
function outputhtml()
{
echo <<<EOT
   <html>
   <head><title>脚本 学堂 主页</title></head>
   <body>主页内容</body>
   </html>
EOT;
}
outputhtml();
?>
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!