就象heredoc结构类似于双引号字符串,Nowdoc结构是类似于单引号字符串的。Nowdoc结构很象heredoc结构,但是 nowdoc不进行解析操作 。 这种结构很适合用在不需要进行转义的PHP代码和其它大段文本。与SGML的 <![CDATA[ ]]> 结构是用来声明大段的不用解析的文本类似,nowdoc结构也有相同的特征。
一个nowdoc结构也用和heredocs结构一样的标记 <<<, 但是跟在后面的标志符要用单引号括起来,就像<<<'EOT'这样。heredocs结构的所有规则也同样适用于nowdoc结构,尤其是结束标志符的规则。
Example #2 Heredoc结构的字符串示例
<?php
$str = <<<EOD
Example of string //注释也会被输出
spanning multiple lines
using heredoc syntax.
EOD;
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;
?>
以上例程会输出:
My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A
Example #6 Nowdoc结构字符串示例
<?php
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
class foo
{
public $foo;
public $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 not print a capital 'A': \x41
EOT;
?>
以上例程会输出:
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41