本文转自: phpchina 原文地址: http://www.phpchina.com/bbs/thread-96882-1-1.html
本来一直在看中文版的php手册,但是谁知道这个中文版的翻译真是漏洞百出的,而且居然连原手册中的例子都能给搞错,暴汗了一下.于是一咬牙,一狠心,一跺脚下载了一个英文的手册.还挺新,12月-5号出的.一口气看到字符串一章,真是爽到家了.就在此时,说时迟那时快,发现字符串一章有了新内容-----nowdoc!!!!!!!!!!!!!!!这还得了,赶紧研究了一下,原来是php5.3.0版新引进的.貌似还没有注意到哦,所以俺就先下手为抢,抢先翻译一下这个新东东.
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML<![CDATA[ ]]> construct, in that it declares a block of text which is not for parsing.
与heredocs针对于双引号字符串相比,nowdoc针对的是单引号内的字符串.虽然nowdoc的定义方式同heredoc相似,但是在一个nowdoc中,变量和转义字符串不会被解析.这种语法结构非常适合在需要嵌入php代码或者其他大量文本块,但是却不必进行转义的时候使用.在某些特性上上nowdoc同SGML的<![CDATA[]]>结构相同,可以在这个结构中声明一段文本块且不会对其进行解析.
A nowdoc is identified with the same <<< seqeuence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
nowdoc使用同heredoc一样的序列,但是在其后所使用的界定符使用单引号包围起来,例如,<<<'EOT'.所有heredoc遵守的规则同样适用于nowdoc, 特别是对于结束界定符的限定.
例子 #4 Nowdoc string quoting example
<?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;
?>
The above example will output:
上面的例子将会输出:
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
Note: Unlike heredocs, nowdocs can be used in any static data context. The typical example is initializing class members or constants:
注意:和heredoc不同,nowdocs可以在任何 静态 数据环境中(对这个地方理解的不是很到位,不晓得译的对不对).比较典型的例子是用在初始化类成员或常数时:
例子 #5 Static data example
<?php
class foo {
public $bar = <<<'EOT'
bar
EOT;
}
?>
Note: Nowdoc support was added in PHP 5.3.0.
注意:Nowdoc在php5.3.0中引入