PHP中Heredoc与NowDoc的优点/不便 [英] Advantages / inconveniences of heredoc vs nowdoc in php

查看:95
本文介绍了PHP中Heredoc与NowDoc的优点/不便的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个新手,建议我最好使用Heredoc而不是太多的嵌套代码(请参阅

As a newbie, I have been advised to preferably use heredoc compared to too many nested codes (see Unexpected T_ELSE in php code).

但是我无法理解Heredoc和nowdoc之间是否有显着差异.

But I can't manage to understand if there is a significant difference between heredoc and nowdoc.

heredoc和nowdoc与另一个对新手来说很重要的优点相比有什么优势(即不是很小的优点,但对我来说很重要).

What would be the advantages for heredoc and nowdoc compared to the other one that would be important for a newbie to understand (i.e. not very minor advantages but important to understand for me).

推荐答案

Nowdocs是单引号字符串,heredocs是双引号字符串. nowdoc的指定方式与heredoc相似,但是在nowdoc内不进行任何解析.该结构非常适合嵌入PHP代码或其他大型文本块,而无需进行转义.

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会更易于使用.就是这样.

If you don't need variable interpolation but need special characters like $ inside your string, Nowdocs are easier to use. That's all.

这篇关于PHP中Heredoc与NowDoc的优点/不便的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆