Introduction to Heredoc and Nowdoc in PHP, phpheredocnowdoc_php tutorial

Source: Internet
Author: User
Tags opening and closing tags cloud hosting

Introduction to Heredoc and Nowdoc in PHP, Phpheredocnowdoc


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.

1. <<

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 ();

The $ variable is automatically replaced in Heredoc and the command and input are placed in a piece to facilitate

Attached: The difference between Heredoc and Nowdoc

Heredoc uses <<< EOT identifiers, and Nowdoc uses identifiers such as <<< ' EOT ', Nowdoc is a new technology introduced by PHP5.3, it contains heredoc syntax, but the content of which is absolutely no escape and interpretation, what content is what content, will not parse PHP related content
PHP variables in Heredoc are recommended to be enclosed in {$name->change ()} braces, which avoids some ambiguity, and if you want to use the legendary escape character \, the escape character itself can be output using an escape character, that is, the representation method, These need to be escaped out of the curly braces.
To ensure that it is indeed available, it is recommended to use Heredoc syntax, which itself is also escaped, because PHP5.3 introduced the Nowdoc syntax, many cloud hosting bad environment will probably not support the cause of the rest of the dish.
Finally, Heredoc is introduced from PHP4.0, and Nowdoc syntax requires 5.3, because Heredoc contains nowdoc functionality, so it is better to use Heredoc for personal advice.

In simple terms:

1, Heredoc is dynamic nowdoc is static
2, heredoc similar to multiple lines of double quotes newdoc similar to multiple lines of single quotation marks
3, Heredoc is a kind of general processing scheme that deals with large-segment string, and Nowdoc is the "high-efficiency" static version of PHP to make up the efficiency problem of "heredoc" in the dynamic implementation.

http://www.bkjia.com/PHPjc/932475.html www.bkjia.com true http://www.bkjia.com/PHPjc/932475.html techarticle PHP Heredoc and nowdoc Introduction, Phpheredocnowdoc Heredoc technology, in regular PHP documents and technical books are not generally described in detail, just mentioned this is a Perl-style string ...

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