PHP delimiter usage Tips _php Tips

Source: Internet
Author: User
Tags parse error
If you use the traditional output method--by string output--there must be a large number of escape characters to escape the special characters in the string, such as quotes, so that there is no syntax error. If it's one or two, you can tolerate it, but if you have a full HTML text or a 200-line JS, I think everyone will crash. That's why PHP introduces a delimiter--at least for a large part of it.
The function of the 1.PHP delimiter is to follow the original, including the line-wrapping format or something, and output it inside;
2. No special characters in the PHP delimiter need to be escaped;
The PHP variable in the 3.PHP delimiter is normally replaced with its value.
The delimiter format in PHP is this:
Copy Code code as follows:

<<<eof
......
Eof;

It looks simple, but there are a lot of places to pay attention to.
The first character EOF after <<< is defined by itself, anything is possible (such as AAA can), but the character at the end must be the same as his, they appear in pairs, like {}--this is the most basic.
The second problem that needs to be noted in the use of PHP delimiters is the most frequent problem:
The end of the line (for example, EOF;), be sure to start a different line, and a row except EOF, the end of this delimiter can not have any other characters, including spaces. If you have a blank or tab at the end of the line, you will receive an error message like this:
Parse error:parse error, unexpected $end in ..., prompts you grammatical error;
The third thing to note is that if there is a PHP variable in the middle of the delimiter, you just need to write as if it were output in other strings, such as
Copy Code code as follows:

<<<eof
hello{$name}
Eof;

Variable $name The reason to use {} is to tell the PHP parser this is a PHP variable, in fact, it is not possible, but there may be ambiguity, such as your variable is not just a letter or a special symbol of what will happen? There's no way to do this.
Copy Code code as follows:

<<<eof
hello<?php Echo $name?>
Eof;

In this case, you will also receive a syntax error message. The first is a field test of the PHP delimiter to the correct wording. It contains code for HTML and javascript:
Copy Code code as follows:

<?php
$name = ' Kitty ';
Echo <<<eof
<table height= ">
<tr><td>
{$name}<br/>
<scr Ipt>
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.