echo <<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結構用在函數參數中來傳輸資料:
Example #3 Heredoc結構在參數中的樣本
var_dump(array(<<foobar!
EOD
));
?>
在PHP 5.3.0以後,也可以用Heredoc結構來初始化靜態變數和類的屬性和常量:
Example #4 使用Heredoc結構來初始化靜態值
// 靜態變數
function foo()
{
static $bar = <<Nothing in here...
LABEL;
}
// 類的常量、屬性
class foo
{
const BAR = <<Constant example
FOOBAR;
public $baz = <<Property example
FOOBAR;
}
?>
在PHP 5.3.0中還在Heredoc結構中用雙引號來聲明標誌符:
Example #5 在heredoc結構中使用雙引號
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
Note: