Home  >  Article  >  Backend Development  >  Delimiter echo<<< in PHP

Delimiter echo<<< in PHP

WBOY
WBOYOriginal
2016-07-30 13:30:271479browse
Since PHP is a Web programming language, you will inevitably encounter the situation of using echo to output large sections of html and javascript scripts during the programming process. If you use the traditional output method-output by string, there must be a large number of The escape character is used to escape special characters such as quotation marks in the string. To avoid grammatical errors, we quote the delimiter "<<<".

1. The function of PHP delimiter is to output the things inside it as it is, including newline format and so on;

2. Any of the PHP delimiters Special characters do not need to be escaped;
3. PHP variables in the PHP delimiter will be replaced with their values ​​normally.

Writing format:

echo << xxx;
where: 1. xxx should meet the requirements for variable names and can be in Chinese;
2. It must exist in an independent line; The delimiter format in PHP is as follows:
There are many places to pay attention to. First of all, the character Eof after <<< is self-defined. Anything is fine, but the characters at the end must be the same. 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 place where problems occur: The line at the end (such as Eof; in the above example) must be started on a new line and changed to another line. There cannot be any other characters except Eof; this delimiter ending identifier, 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; The third requirement Note that if a PHP variable appears in the middle of the delimiter, you only need to write it as you would in other strings, for example:

<< 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 There may be ambiguities. For example, what happens if your variable does not have a letter or special symbol after it? Never write it like this:


<<

hello

Eof;

In this case, you will also receive a syntax error message. Here is the correct way to write the PHP delimiter that has been tested. It contains the code of HTML and JavaScript: & & lt ;? PHP

$ name = 'kitty';

echo & lt; & lt; eoF