How to use Heredoc in PHP _php tutorial

Source: Internet
Author: User
Tags opening and closing tags
Heredoc technology, usually not detailed in regular PHP documentation and technical books, just mentions that this is a Perl-style string output technique. But now some of the forum procedures, and some of the article system, are clever use of heredoc technology, to partially realize the interface and code of the quasi-separation, phpwind template is a typical example.

As follows:
Copy CodeThe code is as follows:
$name = ' Shallow Water tour ';
Print <<


<title>Untitled Document</title>



Hello, $name!


EOT;
?>

1. In <> << < span=""><> Start tag start to EndEnd Tag End, The end tag must be written in the head, cannot have indents and spaces, and has a semicolon at the end of the tag。 The start tag is the same as the start tag, such as EOT, EOD, and EOF, which are commonly capitalized, but are not limited to those, as long as they are guaranteed opening and closing tags do not appear in the body

2. Variables located between the opening and closing tags can be parsed normally, but the function is not . In Heredoc, a variable does not need a connector. Or, to splice, as follows:
Copy the Code code 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 the output contains a large number of HTML syntax D documents. For example: function outputhtml () to output the HTML home page. There are two ways to do this. Obviously the second is simpler and easier to read.
Copy the Code code as follows:
function outputhtml () {
echo "";
echo "Home";
echo "Homepage content";
echo ";
}

function outputhtml ()
{
Echo <<<>

Home
Home Page Content

EOT;
}
Outputhtml ();

http://www.bkjia.com/PHPjc/327655.html www.bkjia.com true http://www.bkjia.com/PHPjc/327655.html techarticle Heredoc technology, usually not detailed in regular PHP documentation and technical books, just mentions that this is a Perl-style string output technique. But now some of the forum programs ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.