tags:

views:

191

answers:

5

Hi,

I have a simple question. 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 doulbe quotion. One of my colleagues told me that it does not matter.

WHat do you guys think?

+2  A: 

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.

Amber
+16  A: 

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?

Pascal MARTIN
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.
Frank Farmer
+1 not using quotes for array keys is stupid and bound to cause trouble one day.
Pekka
@Frank : true ; one more good reason for using the quotes when they should be there !
Pascal MARTIN
+1 as it further explains the description from the manual
Gordon
@Pascal MARTIN: +1 nice, detailed answer. Little off the topic to this question but could you share your ideas to my question here: http://stackoverflow.com/questions/3356376/php-creating-extensible-cms-system
Sarfraz
+2  A: 

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.

Tim Cooper
+4  A: 

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

Darryl Hein
+1  A: 

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.

Gordon