PHP array indexing: $array[$index] vs $array[“$index”] vs $array[“{$index}”]

phpsyntax

What is the difference, if any, between these methods of indexing into a PHP array:

$array[$index]
$array["$index"]
$array["{$index}"]

I'm interested in both the performance and functional differences.

Update:

(In response to @Jeremy) I'm not sure that's right. I ran this code:

  $array = array(100, 200, 300);
  print_r($array);
  $idx = 0;
  $array[$idx] = 123;
  print_r($array);
  $array["$idx"] = 456;
  print_r($array);
  $array["{$idx}"] = 789;
  print_r($array);

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
)

Best Solution

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.