search
HomeDaily ProgrammingPHP KnowledgeWhat are PHP delimiters? what's the effect? (Pictures + Videos)

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 id="name">$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:

What are PHP delimiters? whats the effect? (Pictures + Videos)

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=<<<&#39;ABC&#39;
……
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 = <<<&#39;Now&#39;
<h1 id="name">$name</h1>
<form action=&#39;upload.php&#39; 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:

What are PHP delimiters? whats the effect? (Pictures + Videos)

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use