What is the difference, if any, between these methods of indexing into a php array:
$array[$index]
$array["$index"]
$array["{$index}"]
Thanks!
![]() ![]() ![]() 1
|
|||
|
![]() ![]() ![]() |
see @svec and @jeremy above. 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. |
||||||
|
![]() ![]() |
I timed the 3 ways of using an index like this:
The first set of tests used $idx=0, the second set used $idx="0", and the third set used $idx="blah". Timing was done using microtime() diffs. I'm using WinXP, php 5.2, apache 2.2, and vim. :-) And here are the results:
So $array[$idx] is the hands-down winner of the performance competition, at least on my machine. (The results were very repeatable, btw, I ran it 3 or 4 times and got the same results.) |
|||
|
![]() ![]() |
Don't believe everything you read so blindly... I think you misinterpreted that. The article says $array['index'] is faster than $array[index] where index is a string, not a variable. That's because if you don't wrap it in quotes PHP looks for a constant var and can't find one so assumes you meant to make it a string. |
||
|
![]() ![]() |
That is absolutely not true. |
||
|
![]() ![]() |
So we've covered the performance differences, but what about functional differences? When will the different indexing methods resolve to different indices? |
||
|
![]() ![]() |
According to http://php.net/types.array, an array index can only be an integer or a string. If you try to use a float as an index, it will truncate it to integer. So if $index is a float with the value 3.14, then $array[$index] will evaluate to $array[3] and $array["$index"] will evaluate to $array['3.14']. Here is some code that confirms this:
The output:
|
||
|
![]() ![]() |
If $index is a string there is no difference because $index, "$index", and "{$index}" all evaluate to the same string. If $index is a number, for example 10, the first line will evaluate to $array[10] and the other two lines will evaluate to $array["10"] which is a different element than $array[10]. |
||
|
![]() ![]() |
@Jeremy: I'm not sure that's right. I ran this code:
And got this output: Array ( [0] => 100 [1] => 200 [2] => 300 ) Array ( [0] => 123 [1] => 200 [2] => 300 ) Array ( [0] => 456 [1] => 200 [2] => 300 ) Array ( [0] => 789 [1] => 200 [2] => 300 ) |
|||
|
![]() ![]() |
svec: Oh, you're right, I guess PHP must convert array index strings to numbers if they contain only digits. I tried this code:
And the output was:
I've done some more tests and found that if an array index (or key) is made up of only digits, it's always converted to an integer, otherwise it's a string. ejunker: Can you explain why that's faster? Doesn't it take the interpreter an extra step to parse "$index" into the string to use as an index instead of just using $index as the index? |
||
|
![]() ![]() |
I believe from a performance perspective that $array["$index"] is faster than $array[$index] See Best practices to optimize PHP code performance Another variation that I use sometimes when I have an array inside a string is:
Edit: What I meant to say is $row[’id’] is faster than $row[id] |
|||
|