First thing to realize is that PHP code (that is, code blocks inside PHP
tags) has two very distinct modes - regular code, and code inside double
quotes (there`s also code inside single quotes and backticks, but let`s
skip this for now).
With regular code, associative indices are any valid string expression.
That means a variable that contains a string, a string literal (e.g. "foo")
or some `complex` string expression like $a."foo" or the likes. Labels,
such as foo (without any surrounding quotes) are no longer considered valid
string literals (as of PHP 3.0RC5). Such labels are used for constants,
even though if you use foo, and foo isn`t a valid constant, the label would
be evaluated to the string expression "foo" (and would also generate a
notice-level warning).
To summarize, with regular code, you should use $array["foo"] instead of
$array[foo], even though both would work. If foo is a constant, the 2nd
format has a different meaning from the 1st (e.g. if foo==2, $array[foo] is
identical to $array[2]).
Inside quoted strings, PHP supports a much much more limited range of the
array variable references available in regular code. PHP 3.0 supports the
following formats inside quoted strings, nothing more, nothing less:
$a[7] 7 can be any valid number
$a[label] label can be any valid label, e.g. foo
$a[$b] $b can be any simple scalar variable
The following notation is also available to prevent certain ambiguities
under certain circumstances:
${a[7]}
${a[label]}
${a[$b]}
Now, the interesting part here is that when you want to access an
associative index inside a quoted string, you have only two options - use a
variable as an index (e.g. $b=="foo", $a[$b] == $a["foo"]), or, use a
label, like foo. Using "foo" explicitly will NOT work. Among other
things, this means that not all associative indices can be explicitly
accessed inside quoted strings (e.g., $array["foo bar"] cannot be directly
accessed inside quoted strings, unless you have a scalar variable that
contains the string "foo bar").
So, to extend the summary, inside quoted strings, you would normally access
associative arrays using $a[foo].
Future considerations
---------------------
I guess mostly everybody wants to write code that`ll be compatible with
future versions of PHP 3.x, namely version 3.1. None of us (core
developers) knows what exactly PHP 3.1 would look like, even though we have
plenty of ideas for it. Improving on the variable reference parsing inside
quoted strings is one of the things that`s scheduled for PHP 3.1. We
haven`t yet decided how the behavior would be exactly, but in order to
write safe code that would work under future PHP versions, it`d probably be
wise to follow the following guidelines for array references inside quoted
strings:
* For simple associative indices, use the simple $foo[bar] notation.
* Avoid using ${foo[bar]}. The general idea in PHP 3.1 is to enable
full-featured parsing inside the curly braces, which means foo would be
considered a constant rather than the string literal "foo".
* For `complex` array references, or ambiguous array references (i.e. ones
that require the ${foo[bar]} notation), use the concatenation operator,
e.g. "abcdefg" . $foo["bar"] . "hijklmnop" - this format will definitely
remain intact in future PHP versions.