在 PHP 中可以使用 array[key] 吗?
是否可以使用不带单引号或双引号的数组,例如 $array[key] ?我认为这很糟糕,因为如果我不使用单引号或双引号,PHP 首先查找常量。我的一位同事告诉我,这并不重要。
你们觉得怎么样?
Is it okay to use array without single or double quotion like $array[key]? I thought it is bad because PHP look for constant first if I don't use single or double quotation. One of my colleagues told me that it does not matter.
What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它不被认为是好的——即使它在大多数情况下都有效。
Basically, when PHP sees this :
它将搜索一个用
define
,称为key
——如果没有,则 if 将采用'key'
值。But, if there is something like this earlier in your code :
不再需要了
;相反,它会使用
key
常量的值——并且您的代码将与以下内容相同:In the end, not putting quotes arround the key's name is bad for at least two reasons :
定义
一个名称错误的常量;-)'key'
之前必须搜索常量error_reporting
和display_errors
,通知/警告/错误仍然会生成,即使稍后被丢弃)所以:你不应该在这一点上听那个人的:他错了:这确实很重要。
And if you need some "proof" that's "better" than what people can tell you on stackoverflow, you can point him to this section of the manual, as a reference : **[Why is $foo[bar] wrong?][4]**
It is not considered as OK -- even if it will work in most cases.
Basically, when PHP sees this :
It will search for a constant, defined with
define
, calledkey
-- and, if there is none, if will take the'key'
value.But, if there is something like this earlier in your code :
It will not take
anymore ; instead, it'll use the value of the
key
constant -- and your code will be the same as :In the end, not putting quotes arround the key's name is bad for at least two reasons :
define
a constant with the wrong name ;-)'key'
error_reporting
anddisplay_errors
, the notices/warnings/errors are still generated, even if discarded later)So : you should not listen to that guy on this point : he is wrong : it does matter.
And if you need some "proof" that's "better" than what people can tell you on stackoverflow, you can point him to this section of the manual, as a reference : **[Why is $foo[bar] wrong?][4]**
这是不行的,要添加到其他人所说的内容中,在大多数情况下会触发错误:
请参阅 PHP 手册“为什么 $foo[bar] 错误?”在此页面上的“数组注意事项”下: http://php .net/manual/en/language.types.array.php
This is not okay and to add to what others have said, it will trigger an error in most cases:
See the section in the PHP Manual for "Why is $foo[bar] wrong?" under "Array do's and don'ts" on this page: http://php.net/manual/en/language.types.array.php
这是错误的,并且会自动定义一个常量:
然而,这种用法是正确的:
为了与 PHP2 兼容,在字符串上下文中仍然允许这种旧语法。引用该键会导致解析错误,除非您还使用 { 大括号 } 将其括起来。
This is wrong and will auto-define a constant:
This usage however is correct:
For compatibility with PHP2 this old syntax is still allowed in string context. Quoting the key would lead to a parse error, unless you also use { curly braces } around it.
来自 PHP 手册 - 为什么 $ foo[bar] 错了?
手册中还有一些示例供您查看。
From the PHP Manual - Why is $foo[bar] wrong?
There is some more examples in the manual for you to check out.
除非键实际上是一个常量,否则没有理由不在键周围加上引号。
PHP 的工作方式是查找您输入的常量值,但如果找不到常量,则采用它的字符串表示形式。
如果有人以后编辑您的代码并添加具有该键名的常量,只会引起更多麻烦。
Unless the key actually is a constant, there is no reason for you not to be putting quotes around the key.
The way PHP works is it looks for the constant value of what you've put, but it takes the string representation of it if the constant cannot be found.
If someone were to edit your code down the road and add a constant with that key name, it would just cause more headaches.
这是不好的做法 出于多种原因,不引用键值:
"]"
)。define
需要时间。如果您想避免在名称周围输入引号,而这些名称只是您经常传递的事物的标准元素,那么您可能想改用对象,它采用
object->property
语法而不是$array["element"]
语法。It's bad practice to not quote key values, for a number of reasons:
define
'd constants."]"
).define
's takes time.If you're wanting to avoid typing quotes around names that are just standard elements of a thing you're passing around a lot, perhaps you might want to use objects instead, which take a
object->property
syntax instead of an$array["element"]
syntax.