php Heredoc 结构的字符串示例

 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;

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";
?>

如上的运行结果中:

php Heredoc 结构的字符串示例

Heredoc 结构就象是没有使用双引号的双引号字符串,这就是说在 heredoc 结构中单引号不用被转义,但是上文中列出的转义序列还可以使用。变量将被替换,但在 heredoc 结构中含有复杂的变量时要格外小心。

php

相关推荐