Re: Which array declaration is better?




howachen@xxxxxxxxx wrote:
$t["test"] = "1";
$t[test] = "1";

They are the same, so which style is better and recommended?

Thanks.

Howa

The first one is better, primarily because the second one is not valid
syntax (although, depending on how PHP is configured it might not
complain).

"test" is the string containing the characters t, e, s, and t in that
order. test would refer to a constant (i.e. define('test', 1);). But
if there is no constant called test, PHP will throw the following
notice:

PHP Notice: Use of undefined constant test - assumed 'test'

and he just interprets it as the string "test". But just because you
can't see the error doesn't mean it isn't there :)

.



Relevant Pages