PHP delimiter is supported starting from PHP4.0 version. Then some novice friends may ask what is the PHP delimiter? what's the effect?
In fact, delimiters are used to define formatted large text, and formatting means that the format in the text will be retained, so there is no need to use escape characters in the text. When used, it is followed by an identifier, then formatted text (i.e. a string), and finally the same identifier ends the string.
There are two delimiters in PHP: heredoc (double quote delimiter) and nowdoc (single quote delimiter)
Below we combine simple code examples to introduce you to the relevant knowledge about PHP delimiter in detail.
1. heredoc
heredoc syntax format:
$str=<<<"ABC" …… ABC; echo $str; ?>
Note: The ABC (delimiter) here can be freely defined and placed in double quotes in or without quotation marks, and there cannot be any string (including spaces) after the delimiter.
heredoc usage code example:
<?php // heredoc 和双引号的作用几乎一样 $name = "PHP中文网"; $str1 = <<<Here <h1>$name</h1> <form action="upload.php" method="get"> <input type="checkbox" name="like" value="篮球">篮球<br> <input type="checkbox" name="like" value="足球">足球<br> <input type="checkbox" name="like" value="排球">排球<br> <input type="checkbox" name="like" value="乒乓球">乒乓球<br> <input type="submit" value="确定"> </form> <script>...</script> Here;
Here we write a form code in the delimiter, the effect is as follows:
As shown in the figure, using the delimiter in the heredoc format can parse any content such as variables without adding any escape characters.
2. nowdoc
nowdoc Grammar format:
$str=<<<'ABC' …… ABC; echo $str; ?>
Note: The ABC (delimiter) here can be freely defined and must be Put it in single quotes, and there cannot be any string (including spaces) after the delimiter.
Nowdoc usage code examples are as follows:
<?php // nowdoc 和单引号的作用几乎一样 $name = "PHP中文网"; $str2 = <<<'Now' <h1>$name</h1> <form action='upload.php' method="get"> <input type="checkbox" name="like" value="篮球">篮球<br> <input type="checkbox" name="like" value="足球">足球<br> <input type="checkbox" name="like" value="排球">排球<br> <input type="checkbox" name="like" value="乒乓球">乒乓球<br> <input type="submit" value="确定"> </form> Now; echo $str2;
Similarly, we access through the browser, the effect is as follows:
We can find from the picture that using the delimiter in nowdoc format, cannot parse the variable $name, but there is no need to add any escape characters.
Of course, in addition to the heredoc and nowdoc methods here, there is also our traditional output method, which is to directly output strings in single quotes and double quotes. In this case, there must be a large number of escape characters to escape special characters such as quotation marks in the string to avoid syntax errors.
So to sum up, when we need to output a large amount of html text or js scripts in PHP, PHP delimiters have great advantages, because any special characters in PHP delimiters No escaping is required and PHP variables will be replaced with their values normally.
This article is a detailed explanation of the relevant knowledge about PHP delimiters. I hope it will be helpful to friends in need!
If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!
The above is the detailed content of What are PHP delimiters? what's the effect? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!