php5.3.0尝鲜:新特性小介绍之nowdoc

本文转自: 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中引入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值