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?
It is not considered as OK — even if it will work in most cases.
Basically, when PHP sees this :
It will search for a constant, defined with
define, calledkey— and, if there is none, if will take the'key'value.But, if there is something like this earlier in your code :
It will not take
anymore ; instead, it’ll use the value of the
keyconstant — and your code will be the same as :In the end, not putting quotes arround the key’s name is bad for at least two reasons :
definea constant with the wrong name 😉'key'error_reportinganddisplay_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]**