title: Fastest way to iterate an array in PHP
body: <p>I'm studying for the <a href="https://en.wikipedia.org/wiki/Zend_(company)#Zend_Certification_Test" rel="nofollow noreferrer">Zend PHP certification</a>.</p>
  <p>I am not sure about the answer to this question.</p>
  <blockquote>
  <p><strong>Question:  What is the best way to iterate and modify every element of an array using PHP 5?</strong></p>
  <p>a) You cannot modify an array during iteration</p>
  <p>b) <code>for($i = 0; $i &lt; count($array); $i++) { /* ... */ }</code></p>
  <p>c) <code>foreach($array as $key =&gt; &amp;$val) { /* ... */ }</code></p>
  <p>d) <code>foreach($array as $key =&gt; $val) { /* ... */ }</code></p>
  <p>e) <code>while(list($key, $val) = each($array)) { /* ... */  }</code></p>
  </blockquote>
  <hr />
  <p>My instinctive is (B) since there is no need to create a temporary variable, but then I realize it won't work for associative arrays.</p>
  <p>Further searching around the Internet I found this:</p>
  <p>Storing the invariant array count in a separate variable improves performance.</p>
  <pre><code>$cnt = count($array);
  for ($i = 0; $i &lt; $cnt; $i++) { }
  </code></pre>