PHP delimiter usage tips

Source: Internet
Author: User
Because PHP is a Web programming language, it is inevitable that echo will be used to output large 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 escape characters to escape special characters such as quotation marks in the string to avoid syntax errors. One or two parts can be tolerated, but if it is a complete html text or a 200-line JavaScript code, I think anyone will crash. This is why PHP introduces a separator-at least most of the reasons.
1. PHP delimiters act as they are, including the line feed format or something, and output something inside them;
2. do not escape any special characters in the PHP delimiters;
3. the PHP variable in the PHP delimiter will be replaced with its value normally.
The format of the delimiters in PHP is as follows:

The code is as follows:


< ......
Eof;


It looks simple, but there are many points to note.
First, < <之后的字符eof是自己定义的,随便什么都是可以的(比如aaa都可以),但是结尾处的字符一定要和他一样,他们是成对出现的,就像{}这样的——这是最基本的。
During the use of PHP delimiters, the second issue that requires attention is also the most common one:
The end of a line (Eof; in the preceding example) must start with another line, and change the line except Eof; the end of the separator cannot contain any other characters, including spaces. If there are spaces and tabs at the beginning or end of the line, you will receive the following error message:
Parse error: parse error, unexpected $ end in ......, A syntax error is prompted;
The third thing to note is that 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:

The code is as follows:


< Hello {$ name}
Eof;


The variable $ name should be included in {} to tell the PHP parser that this is a PHP variable. In fact, this variable is not needed, but may be ambiguous, for example, what happens if your variable is not followed by a letter or a special symbol? Never write like this.

The code is as follows:


< Hello
Eof;


In this case, you will also receive a syntax error message. First, it is the correct way to write PHP delimiters tested in the field. It contains the html and javascript code:

The code is as follows:


$ Name = 'Kitty ';
Echo <




{$ Name}

Script
Var p = 'Hello World ';
Document. writeln (p );
Script

Eof;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.