PHP Heredoc and Nowdoc

Source: Internet
Author: User

PHP Heredoc and Nowdoc


Heredoc structure

HEREDOC syntactic structure:<<<. After the operator, provide an identifier, and then wrap the line. The next is the string itself, and finally the identifier defined earlier as the end flag.

<?php$content = <<<FDIPZONE

The HEREDOC structure cannot be used to initialize the properties of a class. Since PHP 5.3, this limit is only valid for Heredoc including variables. The following example will cause an error.

<?phpclass test{public $var = ' 123 ';p ublic $a = <<<fdipzone$varfdipzone;} $obj = new test (); Echo $obj->a;? >

In the HEREDOC structure, the variable is replaced, but the method does not. Take extra care when you have complex variables.

<?php$var = ' 123 '; $content = <<<fdipzone$var time (); Fdipzone;echo $content; 123 time ();? >

Nowdoc Structure

The syntactic structure of nowdoc is very much like the heredoc structure, but the parsing operation is not performed in Nowdoc. Such a structure is ideal for embedding PHP code or other large pieces of text without escaping special characters.

Nowdoc the same tag <<< as the HEREDOC structure, but the following identifiers must be enclosed in a single argument , <<< ' EOF '. The full rules of the HEREDOC structure are the same for nowdoc structures, especially the rules for ending identifiers. Nowdoc was added after the php5.3.

<?php$var = ' 123 '; $content = <<< ' Fdipzone ' $var time (); Fdipzone;echo $content; $var time (); $var have not been replaced?>

The NOWDOC structure can be used in a casual, static data environment, and the most typical demo sample is a property or constant used to initialize a class. The following example will not be error-able to control the Heredoc sample.

<?phpclass test{public $a = <<< ' Fdipzone ' $varFDIPZONE;} $obj = new test (); Echo $obj->a;? >


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.