在PHP中使用array [key]可以吗? [英] Is it okay to use array[key] in PHP?

查看:123
本文介绍了在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.

你们怎么看?

推荐答案

即使在大多数情况下都可以正常使用,它也不被认为是正确的.


基本上,当PHP看到此信息时:


Basically, when PHP sees this :

echo $array[key];

它将搜索用 define 定义的常量,称为 key ;如果没有,则采用'key'值.

It will search for a constant, defined with define, called key -- and, if there is none, if will take the 'key' value.


但是,如果您的代码前面有这样的内容:


But, if there is something like this earlier in your code :

define('key', 'glop');

不需要

echo $array['key'];

了;相反,它将使用 key常量的值-您的代码将与:

anymore ; instead, it'll use the value of the key constant -- and your code will be the same as :

echo $array['glop'];


最后,至少有两个原因,不把引号引起来是不好的:


In the end, not putting quotes arround the key's name is bad for at least two reasons :

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