The do...while Statement


The do...while Statement

 do {     statement } while (expr); 

The PHP do...while loop is very similar to the while loop, except that expr is checked at the end of each iteration instead of at the beginning. Thus, compared to standard while loops, the first iteration of a do...while loop will always run, which is not necessarily true for a while loop (because it depends on the while loop's expression).

Here's an example of using a while loop where the loop condition is false from the very beginning, so this script doesn't echo anything:

 <?php     $value =10;     while ($value < 5){         $value += 2;         echo $value, "<BR>";     } ?> 

However, in a do...while loop, the condition is tested at the end, so this script does echo 12 in a browser:

 <?php     $value = 10;     do{         echo $value, "<BR>";         $value += 2;      } while ($value < 5); ?> 



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net