作为一个新手,我被建议最好使用heredoc,而不是太多的嵌套代码(参见Unexpected T_ELSE in php code)。
但我无法理解 heredoc 和 nowdoc 之间是否存在显着差异。
heredoc 和 nowdoc 与另一个对新手来说很重要的优势相比有什么优势(即不是很小的优势,但对我来说很重要)。
最佳答案
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.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
换句话说:
$foo = 'bar';
$here = <<<HERE
I'm here, $foo !
HERE;
$now = <<<'NOW'
I'm now, $foo !
NOW;
$here
是 “我在这里,酒吧!”,而 $now
是 “我现在, $foo!”。
如果您不需要变量插值但需要在字符串中使用特殊字符,如 $
,Nowdocs 更易于使用。就这样。
关于php - php中heredoc与nowdoc的优点/不便,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11153049/