php - Accessing arrays whitout quoting the key -
i can access array value either $array[key]
or $array['key']
is there reason avoid using 1 on other?
use latter variant $array['key']
. former work because php tolerant , assumes string value key
if there no constant named key:
always use quotes around string literal array index. example,
$foo['bar']
correct, while$foo[bar]
not. […] wrong, works. reason […] has undefined constant (bar
) rather string ('bar'
- notice quotes).
see array do's , don'ts.
now in opposite accessing arrays in plain php code, when using variable parsing in double quoted strings need write without quotes or use curly brace syntax:
[…] inside double-quoted string, it's valid not surround array indexes quotes
"$foo[bar]"
valid. see above examples details on why section on variable parsing in strings.
so:
// syntax error echo "$array['key']"; // valid echo "$array[key]"; echo "{$array['key']}";
Comments
Post a Comment