Is it okay to use array[key] in PHP?

痴心易碎 提交于 2019-11-26 17:48:10

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?

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

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.

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.

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!