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 <<
{$name}< br/> <script> <br>var p='hello world'; <br>document.writeln(p); <br></script>
| < /tr>
Eof;
?>
http://www.bkjia.com/PHPjc/320209.htmlwww.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...