php - php中heredoc与nowdoc的优点/不便

标签 php heredoc nowdoc

作为一个新手,我被建议最好使用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/

相关文章:

PHP: shell_exec();与 cmd 工作不一样? [WIN10命令行]

linux - 如何在 linux ssh here 脚本中禁止评论

bash - 将此处的文档命令通过管道传输到日志文件

mysql - BASH:SSH 进入服务器,查询 MYSQL 有趣的东西

php - 在文本区域上看不到新行 - 问题可能是什么?

php - 搜索具有多个条件的内连接

javascript - 如何忽略 javaScript 或 php 中的 ERR_INSECURE_RESPONSE 错误

php - 测试客户端浏览器中是否安装了客户端证书