<?$vars = "variables";echo <<<EOT
This text will be echoed out,
so will this text, plus you can "use quotation s"
without escaping them! You can also include $vars
directly in your output.
EOT;#EOT stands for "End of Text," and is a commonly used heredoc
#delimiter. If the string "EOT" actually appears somewhere in your
#text, you'll receive errors and not all of your text will be
#printed. You can use any delimiter you like, some programmers
#prefer to use "QQQ" since it's unlikely to show up in real text.?>
This text will be echoed out,
so will this text, plus you can "use quotation s"
without escaping them! You can also include $vars
directly in your output.
EOT;#EOT stands for "End of Text," and is a commonly used heredoc
#delimiter. If the string "EOT" actually appears somewhere in your
#text, you'll receive errors and not all of your text will be
#printed. You can use any delimiter you like, some programmers
#prefer to use "QQQ" since it's unlikely to show up in real text.?>
另一种给字符串定界的方法使用定界符语法(“<<<”)。应该在 <<< 之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。 结束标识符必须从行的第一列开始。同样,标识符也必须遵循 PHP 中其它任何标签的命名规则:只能包含字母数字下划线,而且必须以下划线或非数字字符开始。
警告
很重要的一点必须指出,结束标识符所在的行不能包含任何其它字符,可能除了一个分号(;)之外。这尤其意味着该标识符不能被缩进,而且在分号之前和之后都不能有任何空格或制表符。
定界符文本表现的就和双引号字符串一样,只是没有双引号。这意味着在定界符文本中不需要转义引号,不过仍然可以用以上列出来的转义代码。变量会被展开,但当在定界符文本中表达复杂变量时和字符串一样同样也要注意。 例子 7-2. 定界符字符串例子<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;/* More complex example, with variables. */
class foo
{
var $foo;
var $bar; function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}$foo = new foo();
$name = 'MyName';echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
记住它的结束方法:
echo <<<EOT
....
EOT;这一行的写法是唯一的,即定界符后面跟分号。并且必须处理单独的一行,行首不能有任何字符,行尾也不能有其他字符。