Heredoc and Nowdoc Knowledge in PHP

Source: Internet
Author: User
Tags php code

Heredoc structure

HEREDOC syntactic structure:<<<. After the operator, you supply an identifier and then wrap the line. The string itself is followed by the end flag with the previously defined identifier.

<?php  
$content = <<<fdipzone  

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

<?php  
class test{public  
      
$var = ' 123 ';  
      
Public $a = <<<fdipzone  
$var
fdipzone;  
      
}  
      
$obj = new test ();  
echo $obj->a;  
? >

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

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

Nowdoc structure

NOWDOC syntactic structure is very much like heredoc structure, but Nowdoc does not perform parsing operation. This structure is ideal for embedding PHP code or other large pieces of text without escaping special characters.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

Nowdoc is the same as the HEREDOC structure tag <<<, but the following identifiers must be enclosed in single quotes, i.e. <<< ' EOF '. All rules of the HEREDOC structure apply equally to the NOWDOC structure, especially the rules for closing identifiers. Nowdoc only joined after the php5.3.

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

The NOWDOC structure can be used in any static data environment, and the most typical example is the property or constant used to initialize the class. The following example will not go wrong, and you can compare the Heredoc example.

<?php  
class test{public  
      
$a = <<< ' Fdipzone '
$var
fdipzone;  
      
}  
      
$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.