What's this kind of syntax in PHP?

$test= <<<EOF

....

EOF;

I have never see it before. What's it used for?


This is called HEREDOC syntax , which is a way to define strings, on multiple lines, with variable interpolation.


Quoting the manual page:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

(There is more to read, that I didn't copy-paste from the manual page)


And, as a very quick and simple example:

$a = 'World';
$string = <<<MARKER
<p>
  Hello, $a!
</p>
MARKER;
echo $string;

It will give you this output:

Hello, World!

And this HTML source:

<p>
  Hello, World!
</p>

那将是Heredoc


http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Blockquote A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

链接地址: http://www.djcxy.com/p/1790.html

上一篇: 3个不同的等于

下一篇: PHP中的这种语法是什么?