PHP array indexing: $array[$index] vs $array[“$index”] vs $array[“{$index}”]
What is the difference, if any, between these methods of indexing into a PHP array:
I'm interested in both the performance and functional differences.
Update:
I'm not sure that's right. I ran this code:
And got this output:
|
Solution :
All array indices are of type 'int' first, then type 'string', and will be cast to that as PHP sees fit.
Performance wise, $index should be faster than "$index" and "{$index}" (which are the same).
Once you start a double-quote string, PHP will go into interpolation mode and treat it as a string first, but looking for variable markers ($, {}, etc) to replace from the local scope. This is why in most discussions, true 'static' strings should always be single quotes unless you need the escape-shortcuts like "\n" or "\t", because PHP will not need to try to interpolate the string at runtime and the full string can be compiled statically.
In this case, doublequoting will first copy the $index into that string, then return the string, where directly using $index will just return the string.
source: http://stackoverflow.com/questions/6628/php-array-indexing-arrayindex-vs-arrayindex-vs-arrayindex
COMMENTS