0

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

Is there any significant different between

$test['car'] and $test[car]

Both produce same result.

Community
  • 1
  • 1
cicakman
  • 1,970
  • 4
  • 22
  • 33
  • 2
    The second one is invalid. Turn on `error_reporting()` some time. – Wesley Murch Mar 12 '12 at 08:20
  • 1
    http://php.net/manual/en/language.types.array.php#language.types.array.donts – Brian Roach Mar 12 '12 at 08:21
  • @Mad It's not *invalid*, but in 99.99999% of all cases it's not what the programmer intended. – deceze Mar 12 '12 at 08:22
  • @deceze: Right, only if `car` is a defined constant. My word choice was sub-optimal. It's a common beginner's mistake, taking the relaxed parser for granted and running without error reporting, thus never realizing the warnings. – Wesley Murch Mar 12 '12 at 08:23

4 Answers4

5

In $test[car], car is expected to be a constant. Constants are bare words without quotes or preceding $. If you do not have a constant named car defined, PHP will assume you meant 'car'. That's very bad behavior, but it is what it is. If you'd activate error reporting as you should during development, you'd see a lot of errors pointing this out.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 3
    +1. And the error would be `Notice: Use of undefined constant car - assumed 'car'` – Ben Mar 12 '12 at 08:22
  • you should activate error reporting not only during development but all the time. And on production it's actually way more important. Error displaying you mean. – Your Common Sense Mar 12 '12 at 08:31
  • 1
    @Your Well yes, on production, error *display* should be off. Error *reporting* should be on at all times. Sub-optimal choice of words. – deceze Mar 12 '12 at 08:32
2

They don't necessarily produce the same result. Try this code:

define('car', 'plain');
echo $test['car'] . "\n";
echo $test[car];

The version without the quotes doesn't use the word "car" as the index - instead it uses a constant with the name "car". If that constant is not defined, then the value of the constant is assumed to be the name of it. Hence you get the same results when using it in your case. However in my example, the results will be different.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

The former is correct; the latter is a side effect of a (rather bad) choice of the PHP designers.

The reason why $test[car] works at all and doesn't just crash with a syntax error is because PHP treats bare words (unrecognized symbols that aren't prefixed with $) as constants, and it treats undefined constants as equivalent to a string with the name of the constant. Thus car is equivalent to 'car' if you haven't defined the constant car via define().

Please, for the love of all that is good and happy, do not rely on this functionality. It is a potential maintenance nightmare waiting to happen. (And in fact, if you have display of Notice-level messages turned on PHP, you'll see a message about the usage of an undefined constant.)

Use $test['car'].

Community
  • 1
  • 1
Amber
  • 507,862
  • 82
  • 626
  • 550
-1

There is no difference for the arrays

There is the difference for the strings that happened to be array keys.

So, using just 'car' and car without arrays will have exactly the same effect.

'car' is a string and car is an extremely bad PHP feature: it is actually a constant, which, it none of that name found, treated as a string.

The output though is not the same, $test[car] will produce an error message. An should be never, ever used.

As for the arrays - almost any PHP statement can be used inside of [] braces - a string, a variable, a constant, a logical operator, a function call, etc.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345