Home > Backend Development > PHP Tutorial > The differences and characteristics between php heredoc and nowdoc

The differences and characteristics between php heredoc and nowdoc

jacklove
Release: 2023-03-31 09:10:01
Original
1587 people have browsed it

php heredoc and nowdoc

heredoc structure

heredoc syntax Structure: <<<. After the operator, provide an identifier and then a newline. Next comes the string itself, ending with the previously defined identifier.

<?php
$content = <<<FDIPZONE
<html>
<head>
<title>test</title>
</head>
<body>
<p><img src="http://www.csdn.net/css/logo.png"></p>
<p><a href=&#39;http://blog.csdn.net/fdipzone&#39;>blog</a></p>
</body>
</html>
FDIPZONE;
echo $content;
?>
Copy after login

The heredoc structure cannot be used to initialize properties of a class. As of PHP 5.3, this restriction only applies when the heredoc contains variables. The following example will go wrong.

<?php
class test{
public $var = &#39;123&#39;;
public $a = <<<FDIPZONE
$var
FDIPZONE;
}
$obj = new test();
echo $obj->a;
?>
Copy after login

In the heredoc structure, variables will be replaced, but methods will not. Be careful when containing complex variables.

<?php
$var = &#39;123&#39;;
$content = <<<FDIPZONE
$var time();
FDIPZONE;
echo $content; // 123 time();
?>
Copy after login

nowdoc structure

##nowdoc syntactic structure is very similar to the heredoc structure, but in nowdoc No parsing operation is performed. This structure is suitable for embedding PHP code or other large pieces of text without escaping special characters.

nowdoc The same tags <<< as in the heredoc structure, but the identifier following must be enclosed in single quotes, that is, <<<'EOF'. All the rules for heredoc structures also apply to nowdoc structures, especially the rules for terminating identifiers. nowdoc was added after php5.3.

<?php
$var = &#39;123&#39;;
$content = <<<&#39;FDIPZONE&#39;
$var time();
FDIPZONE;
echo $content; // $var time(); $var没有被替换
?>
Copy after login

nowdoc structure can be used in any static data environment. The most typical example is to initialize properties or constants of a class. The following example will not go wrong, you can compare it with the heredoc example.

<?php
class test{
public $a = <<<&#39;FDIPZONE&#39;
$var
FDIPZONE;
}
$obj = new test();
echo $obj->a;
?>
Copy after login
This article explains the differences and characteristics between php heredoc and nowdoc. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Explanation on php zip file content comparison

About the difference between HTML5 localStorage and sessionStorage Difference

How to get/set the language class of the user access page through php

The above is the detailed content of The differences and characteristics between php heredoc and nowdoc. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template