Heredoc and nowdoc in php

Source: Internet
Author: User
Tags cloud hosting
This article mainly introduces HEREDOC in php, and describes the differences between nowdoc and heredoc in detail. We recommend this article for your reference.

This article mainly introduces HEREDOC in php, and describes the differences between nowdoc and heredoc in detail. We recommend this article for your reference.

Heredoc technology is generally not detailed in regular PHP documents and technical books. It only mentions that it is a Perl-style string output technology. However, some forum programs and some article systems use heredoc to partially implement the quasi-separation of interfaces and codes. phpwind template is a typical example.

1. Use <

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 concatenated with connectors. Or, as shown below:

The Code is as follows:


$ V = 2;
$ A = < "Abc" $ v
"123"
EOF;
Echo $ a; // The result is output together with double quotation marks: "abc" 2 "123"


3. heredoc is often used to output a large number of HTML syntax d documents. For example, the outputhtml () function outputs the HTML homepage. There can be two writing methods. Obviously, the second method is relatively simple and easy to read.

The Code is as follows:


Function outputhtml (){
Echo"";
Echo"Home Page";
Echo"Homepage content";
Echo";
}
Function outputhtml ()
{
Echo <
Home Page
Homepage content

EOT;
}
Outputhtml ();

In heredoc, the $ variable is automatically replaced, and the command and input are placed in one segment for convenience.

Appendix: differences between heredoc and nowdoc

Heredoc uses the <EOT identifier, while nowdoc uses the <'eot' identifier. nowdoc is a new technology introduced in PHP5.3 and contains the heredoc syntax, however, the content will never be converted or interpreted. What is the content and PHP-related content will not be parsed.
We recommend that you enclose the PHP variable {$ name-> change ()} in brackets in heredoc to avoid ambiguity. If you want to output the variable as is, you can use the legendary Escape Character \, escape characters can be output using escape characters, that is, the \ representation method. Escape output is required for all the braces.
To ensure availability, we recommend that you use the heredoc syntax, which is also escaped. Because the nowdoc syntax introduced by PHP5.3, it is very likely that it is not supported by the multi-cloud hosting environment, resulting in a break.
At last, heredoc was introduced from PHP4.0, while nowdoc requires Version 5.3. Because heredoc contains the nowdoc function, it is recommended that heredoc be used.

To put it simply:

1. heredoc is dynamic and nowdoc is static.
2. heredoc is similar to the double quotation marks of multiple rows. newdoc is similar to the single quotation marks of multiple rows.
3. heredoc is a general solution dedicated to processing large string segments, while nowdoc is a static version of php that achieves "High Efficiency" to make up for the dynamic implementation of the "heredoc" Efficiency problem.

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.