码迷,mamicode.com
首页 > 其他好文 > 详细

Heredoc和Nowdoc

时间:2014-09-15 15:30:09      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:io   ar   sp   cti   代码   on   c   amp   line   

就象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

Heredoc和Nowdoc

标签:io   ar   sp   cti   代码   on   c   amp   line   

原文地址:http://www.cnblogs.com/jamesbd/p/3972697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!