有没有在PHP裸字符串的合法使用? [英] Is there any legitimate use for bare strings in PHP?

查看:194
本文介绍了有没有在PHP裸字符串的合法使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题让我想起了光秃秃的字符串。

This question got me thinking about bare strings.

当PHP看到,这不是用引号括起来的字符串,它首先检查,看它是否是一个常数。如果不是,它只是假定它是一个字符串,反正下去。因此,举例来说,如果我有

When PHP sees a string that's not enclosed in quotes, it first checks to see if it's a constant. If not, it just assumes it's a string and goes on anyway. So for example if I have

echo $foo[bar];

如果有它使用的数组键恒定叫吧,但如果不是那么它把酒吧作为一个字符串裸露,所以它的行为就像

If there's a constant called bar it uses that for the array key, but if not then it treats bar as a bare string, so it behaves just like

echo $foo["bar"];

这可引起各种问题,如果在未来的某个时间加入具有相同名称的恒定

This can cause all kinds of problems if at some future date a constant is added with the same name.

我的问题是,有没有在它实际上是有意义的使用裸字符串的任何情况吗?

My question is, is there any situation in which it actually makes sense to use a bare string?

推荐答案

不,我不能看到一个实例,其中是明智的,它总是很危险。使用字符串不含引号绝对应该保留,以解决常数。我不明白怎么PHP的发明者可以决定在所有介绍的这款可笑的行为 - 它使正确使用常量几乎是不可能的(因为如果你试图访问一个尚未定义的常量,PHP会悄悄地和愚蠢地产生字符串)不给的任何的好处。

Nope, I can not see a single instance where it would make sense, and it always is dangerous. Using strings without quotes should absolutely be reserved to address constants. I don't understand how the inventors of PHP could decide to introduce this ridiculous behaviour at all - it makes the proper use of constants almost impossible (because if you try to access a constant that has not been defined, PHP will silently and stupidly generate a string) without giving any benefit.

这篇关于有没有在PHP裸字符串的合法使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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