Login | Contact Us | Site Map | Resources

PHP Conditions & Loops

Home > PHP Tutorial > Do-While

Do-While

The do-while loop is very similar to the while loop, with one major difference. The condition is checked after running each loop instead of before. The "do" part comes before the "while" part, so the block of code is run once even if the condition is false.

An example is:

<?php
  $count = 1;
  do {
    echo $count . "<br>";
    $count++;
  }
  while ($count<=5);
?>

This example results in the numbers 1-5 being echoed on separate lines.

The do-while loop is not used often. At this point in my career, I have yet to use it, but now you know that it exists if you ever need it!