在 PHP 中可以使用 array[key] 吗?

发布于 2024-08-24 07:41:33 字数 110 浏览 10 评论 0原文

是否可以使用不带单引号或双引号的数组,例如 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

两人的回忆 2024-08-31 07:41:33

它不被认为是好的——即使它在大多数情况下都有效。

Basically, when PHP sees this :

echo $array[key];

它将搜索一个用 define,称为 key ——如果没有,则 if 将采用 'key' 值。

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

define('key', 'glop');

不再需要了

echo $array['key'];

;相反,它会使用key常量的值——并且您的代码将与以下内容相同:

echo $array['glop'];

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

  • 存在它不会执行以下操作的风险你期望的--这非常糟糕
    • 今天可能...
    • 但是下周/下月/下年呢?
    • 也许有一天,您会定义一个名称​​错误的常量;-)

  • 这对性能不利:
    • 在使用'key'之前必须搜索常量
    • 并且,正如评论中所述,它会生成通知(即使您禁用error_reportingdisplay_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 :

echo $array[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');

It will not take

echo $array['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 :

  • There is a risk that it will not do what you expect -- which is very bad
    • It might, today...
    • But what about next week / month / year ?
    • Maybe, one day, you'll define a constant with the wrong name ;-)
  • It's not good for performance :
    • it has to search for a constant, before using 'key'
    • And, as said in a comment, it generates notices (even if you disable error_reporting and display_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]**

盛夏已如深秋| 2024-08-31 07:41:33

这是不行的,要添加到其他人所说的内容中,在大多数情况下会触发错误:

8 Notice Use of undefined constant key - assumed 'key' in file: 'index.php' on line 46

请参阅 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:

8 Notice Use of undefined constant key - assumed 'key' in file: 'index.php' on line 46

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

墨落画卷 2024-08-31 07:41:33

这是错误的,并且会自动定义一个常量:

$var = $array[bar];

然而,这种用法是正确的:

$var = "string $array[bar] ...";

为了与 PHP2 兼容,在字符串上下文中仍然允许这种旧语法。引用该键会导致解析错误,除非您还使用 { 大括号 } 将其括起来。

This is wrong and will auto-define a constant:

$var = $array[bar];

This usage however is correct:

$var = "string $array[bar] ...";

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.

南…巷孤猫 2024-08-31 07:41:33

来自 PHP 手册 - 为什么 $ foo[bar] 错了?

始终在字符串文字数组索引周围使用引号。例如,$foo['bar'] 是正确的,而 $foo[bar] 则不正确。但为什么?在旧脚本中经常遇到这种语法:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

这是错误的,但它有效。原因是这段代码有一个未定义的常量(bar)而不是一个字符串('bar' - 注意引号)。 PHP 将来可能会定义常量,但不幸的是,对于此类代码来说,这些常量具有相同的名称。它之所以有效,是因为 PHP 会自动将裸字符串(不带引号的字符串,不对应于任何已知符号)转换为包含裸字符串的字符串。例如,如果没有定义名为 bar 的常量,则 PHP 将替换字符串“bar”并使用它。

手册中还有一些示例供您查看。

From the PHP Manual - Why is $foo[bar] wrong?

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

There is some more examples in the manual for you to check out.

清泪尽 2024-08-31 07:41:33

除非键实际上是一个常量,否则没有理由不在键周围加上引号。

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.

z祗昰~ 2024-08-31 07:41:33

这是不好的做法 出于多种原因,不引用键值:

  1. 与有意义的符号名称(例如定义的常量)的潜在冲突。
  2. 某些键无法在不加引号的情况下表达(例如,键 "]")。
  3. 坏习惯可能会在以后困扰你(即关于#1和#2)。
  4. 性能 - 搜索define需要时间。

如果您想避免在名称周围输入引号,而这些名称只是您经常传递的事物的标准元素,那么您可能想改用对象,它采用 object->property 语法而不是 $array["element"] 语法。

It's bad practice to not quote key values, for a number of reasons:

  1. Potential collisions with meaningful symbol names, such as define'd constants.
  2. Some keys can't be expressed without quoting (for instance, the key "]").
  3. Bad habits can bite you later on (namely in regards to #1 and #2).
  4. Performance - searching for 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文