I see this alot in hacks and the vb code in general, so I thought I'd quote the PHP manual:
Quote:
You might have seen the following syntax in old scripts:
PHP Code:
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
This is wrong, but it works. Then, why is it wrong? The reason is that, as stated in the syntax section, there must be an expression between the square brackets ('[' and ']').
That means that you can write things like this:
PHP Code:
echo $arr[ foo(true) ];
This is an example of using a function return value as the array index. PHP knows also about constants, and you may have seen the E_* before.
PHP Code:
$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";
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:
PHP Code:
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
because E_ERROR equals 1, etc.
Then, how is it possible that $foo[bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant bar literally, as the string "bar", but that you forgot to write the quotes.
So why is it bad then?
At some point in the future, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special keywords.
And, if these arguments don't help: this syntax is simply deprecated, and it might stop working some day.
Note: When you turn error_reporting to E_ALL, you will see that PHP generates warnings whenever this construct is used. This is also valid for other deprecated 'features'. (put the line error_reporting(E_ALL); in your script)
Note: Inside a double-quoted string, an other syntax is valid. See variable parsing in strings for more details.