In
PHPIn the development we will be very often used to HTML, and sometimes a lot of HTML, directly in
PHPTo write HTML is very inconvenient, I believe many students have met, do not worry
PHPThe delimiters in Heredoc and Nowdoc will help us, so let's take a look!
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 restriction is only valid for heredoc containing variables. The following example will cause 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. 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 much like the heredoc structure, but the parsing operation is not performed in Nowdoc. This 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 single quotes, i.e. <<< ' EOF '. All the rules of the HEREDOC structure also apply to the NOWDOC structure, especially the rule that ends the identifier. 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 any static data environment, the most typical example being a property or constant used to initialize a class. The following example does not make an error and can be compared to the heredoc example.
<?php class test{public $a = <<< ' Fdipzone ' $var fdipzone; } $obj = new test (); echo $obj->a; ? >