Free Web Tutorials Free Online Tutorials About Us Contact Us

PHP While Loops Tutorial:


while loops are very popular in PHP and are quiet easy to use. while loops execute a piece of code a specified number of times as set out in the condition.

As long as the condition is true the code will be executed, when condition become false the code is not execute anymore.

Example:

<?php

$counter = 0;
while ($counter < 4) {
   echo "Counter value is = $counter 
"; $counter++; } ?>


The output from the above piece of code would be

Counter value is = 0 Counter value is = 1 Counter value is = 2 Counter value is = 3

At the end of the block execution, the while condition is tested again. If the condition is true the code block will be executed again. This will continue until the condition results becomes false.

When this happens $counter will equal 4, the condition will return false and the loop will be broken and the next line after the while loop is executed.

php loops tutorial