title: do-while is the fastest loop in php?
body: <p>I have profiled <em>for</em>, <em>while</em> and <em>do-while</em> loops with something simple:</p>
  <pre><code>while ($var &lt; 1000000) {
    ++$var;
  }
  
  do {
    ++$var;
  } while ($var &lt; 1000000);
  
  for ($var = 0; $var &lt; 1000000; ++$var) {
    //do nothing
  }
  </code></pre>
  <p>by comparing microtime() before and after the loops.</p>
  <p>The do-while loop is by a considerable amount the fastest loop. <em>do-while</em> is actually faster than <em>while</em> by almost half. I know that they are for different purposes ( <em>while</em> checks the condition before the loop executes and <em>do-while</em> executes at least once ).</p>
  <p>I know the general consensus is that while loops are frowned upon and do-while even more so.</p>
  <p>My question is why? Considering how many for loops are used in PHP applications, shouldn't <em>do-while</em> be used more? Even with an <em>if</em> statement to check a condition before the loop executes, the performance boost is considerable.</p>
  <p>My currently accepted answer is that code legibility is the suspect.</p>
  <p><strong>10 year Edit:</strong>
  10 years ago, I was asked this question at a job interview. I went into the interview with the incorrect perception that while loops were frowned upon. I learned this from my previous job where while loops were not allowed in code, as per instruction from my superior.</p>
  <p>The interview had gone well with management, then I was handed over to the lead programmer, he asked me what the fastest loop in PHP was, which I got incorrect and I did not get the job, this is why I asked the question on SO.</p>
  <p>10 years of experience has taught me a lot.</p>
  <ol>
  <li>while loops are fine (its ludicrous to think I was taught otherwise)</li>
  <li>Micro-optimizations are truly evil (profile code, focus on bottle necks)</li>
  <li>in 10 years, I have never re-written a loop for speed. I have however rewritten the logic inside the loop which was always the true bottleneck.</li>
  <li>There is a lot of strongly held but largely incorrect opinions in the programming field. Stay the course, read up, experiment and question, be open to learning from better programmers and dont be afraid to be wrong.</li>
  </ol>