Example #1 非法的示例
<?php
class foo {
public $bar = <<<EOT
bar
EOT;
}
?>
Heredoc结构就象是没有使用双引号的双引号字符串, 这就是说在heredoc结构中引号不用被替换,但是上文中列出的字符 (\n等)也可使用。 变量将被替换,但在heredoc结构中字符串表达复杂变量时,要格外小 心。
Example #2 Heredoc结构的字符串示例
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
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;
?>
以上例程会输出:
My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A
也可以把Heredoc结构用在函数参数中来传输数据: