Windows XP application tips PHP delimiter usage tips

WBOY
Release: 2016-07-29 08:40:06
Original
1058 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 the PHP delimiter is to output what is inside it as it is, including newline format and so on;
2. Any special characters in the PHP delimiter do not need to be escaped;
3.PHP definition PHP variables within delimiters will be replaced with their values ​​normally.
The delimiter format in PHP is like this:

Copy the code The code is as follows:


<<...
Eof;


Looks very simple, but there are There are many places to pay attention to.
First of all, the character Eof after <<< is defined by yourself. Anything is acceptable (such as AAA), but the characters at the end must be the same as him. They appear in pairs, just Something like {} - this is the most basic.
In the process of using PHP delimiters, the second issue that needs attention is also the most common place where problems occur:
The last line (such as Eof; in the above example) must be started on a new line, and the line must be changed except for Eof ; There cannot be any other characters outside the end of this delimiter, and there cannot be any characters before or after it, 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;
The third one needs attention. The thing is, if there is a PHP variable in the middle of the delimiter, you only need to write it like output in other strings, for example

Copy the code The code is as follows:


<<< ;Eof
hello{$name}
Eof;


The reason why the variable $name is enclosed in {} 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 there is not a letter or special symbol behind your variable? Never write like this

Copy the code The code is as follows:


<<hello
Eof;


In this case, you 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 code:

Copy code The code is as follows:


$name = 'kitty';
echo <<



{$name}

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template