<p>What is the difference, if any, between these methods of indexing into a PHP array:</p>
<pre><code>$array[$index]
$array[&quot;$index&quot;]
$array[&quot;{$index}&quot;]
</code></pre>
<p>I'm interested in both the performance and functional differences.</p>
<h3>Update:</h3>
<p>(In response to @Jeremy) I'm not sure that's right. I ran this code:</p>
<pre><code>  $array = array(100, 200, 300);
  print_r($array);
  $idx = 0;
  $array[$idx] = 123;
  print_r($array);
  $array[&quot;$idx&quot;] = 456;
  print_r($array);
  $array[&quot;{$idx}&quot;] = 789;
  print_r($array);
</code></pre>
<p>And got this output:</p>
<pre><code>Array
(
    [0] =&gt; 100
    [1] =&gt; 200
    [2] =&gt; 300
)

Array
(
    [0] =&gt; 123
    [1] =&gt; 200
    [2] =&gt; 300
)

Array
(
    [0] =&gt; 456
    [1] =&gt; 200
    [2] =&gt; 300
)

Array
(
    [0] =&gt; 789
    [1] =&gt; 200
    [2] =&gt; 300
)
</code></pre>

[{'answer_id': 6629, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 0, 'selected': False, 'text': '<p>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].</p>\n'}, {'answer_id': 6637, 'author': 'ejunker', 'author_id': 796, 'author_profile': 'https://Stackoverflow.com/users/796', 'pm_score': -1, 'selected': False, 'text': '<p>I believe from a performance perspective that $array["$index"] is faster than $array[$index] See <a href="http://www.chazzuka.com/blog/?p=163" rel="nofollow noreferrer">Best practices to optimize PHP code performance</a></p>\n\n<p>Another variation that I use sometimes when I have an array inside a string is:</p>\n\n<pre><code>$str = "this is my string {$array["$index"]}";\n</code></pre>\n\n<p>Edit: What I meant to say is $row[’id’] is faster than $row[id]</p>\n'}, {'answer_id': 6646, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 1, 'selected': False, 'text': "<h3>Response to the Update:</h3>\n<p>Oh, you're right, I guess PHP must convert array index strings to numbers if they contain only digits. I tried this code:</p>\n<pre><code>$array = array('1' =&gt; 100, '2' =&gt; 200, 1 =&gt; 300, 2 =&gt; 400);\nprint_r($array);\n</code></pre>\n<p>And the output was:</p>\n<pre><code>Array([1] =&gt; 300 [2] =&gt; 400)\n</code></pre>\n<p>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.</p>\n<p>ejunker:</p>\n<p>Can you explain why that's faster? Doesn't it take the interpreter an extra step to parse &quot;$index&quot; into the string to use as an index instead of just using $index as the index?</p>\n"}, {'answer_id': 6649, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': True, 'text': '<p>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.</p>\n\n<p>Performance wise, $index should be faster than "$index" and "{$index}" (which are the same).  </p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 6650, 'author': 'svec', 'author_id': 103, 'author_profile': 'https://Stackoverflow.com/users/103', 'pm_score': 5, 'selected': False, 'text': '<p>I timed the 3 ways of using an index like this:</p>\n\n<pre><code>for ($ii = 0; $ii &lt; 1000000; $ii++) {\n   // TEST 1\n   $array[$idx] = $ii;\n   // TEST 2\n   $array["$idx"] = $ii;\n   // TEST 3\n   $array["{$idx}"] = $ii;\n}\n</code></pre>\n\n<p>The first set of tests used <code>$idx=0</code>, the second set used <code>$idx="0"</code>, and the third set used <code>$idx="blah"</code>.  Timing was done using <code>microtime()</code> diffs.  I\'m using WinXP, PHP 5.2, Apache 2.2, and Vim.  :-)</p>\n\n<p>And here are the results:</p>\n\n<h3>Using <code>$idx = 0</code></h3>\n\n<pre><code>$array[$idx]            // time: 0.45435905456543 seconds\n$array["$idx"]          // time: 1.0537171363831 seconds\n$array["{$idx}"]        // time: 1.0621709823608 seconds\nratio "$idx" / $idx     // 2.3191287282497\nratio "{$idx}" / $idx   // 2.3377348193858\n</code></pre>\n\n<h3>Using <code>$idx = "0"</code></h3>\n\n<pre><code>$array[$idx]            // time: 0.5107250213623 seconds\n$array["$idx"]          // time: 0.77445602416992 seconds\n$array["{$idx}"]        // time: 0.77329802513123 seconds\nratio "$idx" / $idx     // = 1.5163855142717\nratio "{$idx}" / $idx   // = 1.5141181512285\n</code></pre>\n\n<h3>Using <code>$idx = "blah"</code></h3>\n\n<pre><code>$array[$idx]           // time: 0.48077392578125 seconds\n$array["$idx"]         // time: 0.73676419258118 seconds\n$array["{$idx}"]       // time: 0.71499705314636 seconds\nratio "$idx" / $idx    // = 1.5324545551923\nratio "{$idx}" / $idx  // = 1.4871793473086\n</code></pre>\n\n<p>So <code>$array[$idx]</code> 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.)</p>\n'}, {'answer_id': 6657, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>When will the different indexing methods resolve to different indices?</p>\n</blockquote>\n\n<p>According to <a href="http://php.net/types.array" rel="nofollow noreferrer">http://php.net/types.array</a>, 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 <code>$index</code> is a float with the value 3.14, then <code>$array[$index]</code> will evaluate to <code>$array[3]</code> and <code>$array["$index"]</code> will evaluate to <code>$array[\'3.14\']</code>. Here is some code that confirms this:</p>\n\n<pre><code>$array = array(3.14 =&gt; \'float\', \'3.14\' =&gt; \'string\');\nprint_r($array);\n\n$index = 3.14;\necho $array[$index]."\\n";\necho $array["$index"]."\\n";\n</code></pre>\n\n<p>The output:</p>\n\n<pre><code>Array([3] =&gt; float [3.14] =&gt; string)\nfloat\nstring\n</code></pre>\n'}, {'answer_id': 6677, 'author': 'Akira', 'author_id': 795, 'author_profile': 'https://Stackoverflow.com/users/795', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>I believe from a performance\n  perspective that $array["$index"] is\n  faster than $array[$index] See Best\n  practices to optimize PHP code\n  performance</p>\n</blockquote>\n\n<p>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 <strong>string</strong>, 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.</p>\n'}]
['https://Stackoverflow.com/questions/6628', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/103/']