Why does PHP's foreach advance the pointer of its array (only) once?
<p>This is a question of curiosity about the reasons behind the way <code>foreach</code> is implemented within PHP.</p>

<p>Consider:</p>

<pre><code>$arr = array(1,2,3);
foreach ($arr as $x) echo current($arr) . PHP_EOL;
</code></pre>

<p>which will output:</p>

<pre><code>2
2
2
</code></pre>

<p>I understand that <code>foreach</code> rewinds array pointers to the beginning; however, why does it then increment it only once? What is happening inside the magic box?? Is this just an (ugly) artefact?</p>

<hr>

<p>Thanks @NickC -- for anyone else curious about <code>zval</code> and <code>refcount</code>, you can read up on the basics <a href="http://php.net/manual/en/features.gc.refcounting-basics.php" rel="nofollow">here</a></p>
