Why
is
$foo[
bar
]
wrong
?
This is wrong, but it works. Then, why is it wrong? The
reason is that this code has an undefined constant (bar)
rather than a string ('bar' - notice the quotes), and PHP may
in future define constants which, unfortunately for your
code, have the same name. It works, because the undefined
constant gets converted to a string of the same name
automatically for backward compatibility reasons.
This is an example of using a function return value as the
array index. PHP also knows about constants, as you may have
seen the
E_*
ones before.
Note that
E_ERROR
is also a valid identifier, just like
bar
in the first example. But the last example is in fact the
same as writing:
because
E_ERROR
equals
1
, etc.