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

6

23

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?

Straw answered 8/3, 2010 at 23:3 Comment(1)
Perhaps your colleague meant it does not matter whether to use single or double quotes? Because that would be more or less true (except that variables don't get parsed in single quotes.)Dressage
C
42

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]**
Cab answered 8/3, 2010 at 23:6 Comment(4)
Also, every time PHP sees this syntax, it throws an E_NOTICE, which has some performance overhead. And probably writes some spam out to your log. Because you should be logging and resolving notice-level errors.Helyn
+1 not using quotes for array keys is stupid and bound to cause trouble one day.Dressage
@Frank : true ; one more good reason for using the quotes when they should be there !Cab
"It will not take [code]echo $array['key'];[/code] anymore"...should be "It will not take [code]echo $array[key];[/code] anymore."Hatband
S
6

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

Secern answered 8/3, 2010 at 23:9 Comment(0)
M
4

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.

Marine answered 20/2, 2011 at 19:47 Comment(0)
W
3

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.

Willettewilley answered 8/3, 2010 at 23:10 Comment(0)
S
2

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.

Sandon answered 8/3, 2010 at 23:6 Comment(0)
E
2

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.

Expansible answered 8/3, 2010 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.