PHP delimiter usage tips

Source: Internet
Author: User
Tags parse error

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:
Copy codeThe Code is as follows:
<Eof
......
Eof;

It looks simple, but there are many points to note.
First, the <character Eof is defined by yourself. Anything can be done (for example, AAA can be used), but the character at the end must be the same as that at the end, they appear in pairs, like {} -- this is the most basic.
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:
Copy codeThe Code is as follows:
<Eof
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.
Copy codeThe Code is as follows:
<Eof
Hello <? Php echo $ name?>
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:
Copy codeThe Code is as follows:
<? Php
$ Name = 'Kitty ';
Echo <Eof
<Table height = "20">
<Tr> <td>
{$ Name} <br/>
<Script>
Var p = 'Hello world ';
Document. writeln (p );
</Script>
</Td> </tr>
</Table>
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.