Home > Backend Development > PHP Tutorial > PHP delimiter usage tips_PHP tutorial

PHP delimiter usage tips_PHP tutorial

WBOY
Release: 2016-07-21 15:45:55
Original
1352 people have browsed it

If you use the traditional output method - output by string, you must have a large number of escape characters to escape special characters such as quotation marks in the string to avoid syntax errors. If it is one or two places, it can be tolerated, but if it is a complete html text or a 200-line js, I think everyone will collapse. This is why PHP introduces a delimiter - at least a large part of the reason.
1. The function of PHP delimiter is to output what is inside it as it is, including newline format and so on;
2. Any special characters in PHP delimiter do not need to be escaped;
3. PHP variables in PHP delimiters will be replaced with their values ​​normally.
The delimiter format in PHP is like this:

Copy code The code is as follows:

<<
Eof;

It seems simple, but there are many things to pay attention to.
First of all, the character Eof after <<< is defined by yourself. Anything can be used (such as AAA), but the characters at the end must be the same as him. They appear in pairs. , like {} - this is the most basic.
In the process of using PHP delimiters, the second issue that needs attention is also the most common problem:
The line at the end (such as Eof; in the above example) must be started on a new line. And the line change cannot have any other characters except the end of the delimiter Eof;, neither before nor after, including spaces. If spaces or tabs appear at the beginning or end of the line, you will receive an error message like this:
Parse error: parse error, unexpected $end in..., prompting you for a syntax error;
No. Three things to note are that if a PHP variable appears in the middle of the delimiter, you only need to write it like you output it in other strings, such as
Copy code The code is as follows:

<<hello{$name}
Eof;

of variable $name So the purpose of enclosing it with {} is to tell the PHP parser that this is a PHP variable. In fact, it is okay not to use it, but it may cause ambiguity. For example, what happens if your variable does not happen to have a letter or a special symbol after it? ? Never write like this
Copy code The code is as follows:

<<hello< ;?php echo $name?>
Eof;

In this case, you will also receive a syntax error message. The first is the correct way to write the PHP delimiter that has been tested in the field. It contains html and javascript codes:
Copy code The code is as follows:

$ name = 'kitty';
echo <<
< /tr>

{$name}< br/>
<script> <br>var p='hello world'; <br>document.writeln(p); <br></script>

Eof;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320209.htmlTechArticleIf you use the traditional output method - output by string, you must have a large number of escape characters to Special characters such as quotation marks in the string are escaped to avoid syntax errors. If...
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