"Loop statements are specifically designed to enable you to perform repetitive tasks because they can continue to operate until a specified condition is achieved or until you explicitly choose to exit the loop." (117) The while statement LOOKS similar to the if statement, but it adds the ability to loop. In fact, it will continue to loop for as long as the condition evaluates to true. "Each execution of a code block within a loop is called an interation." (118) However - beware of the infinite loop! This is the loop that never ends, because the code block never becomes false. This is why counter variables are so useful - they set an absolute end to the loop, and avoid the dangers of infinitely executing (and thereby crashing the computer!). This example uses a counter variable which begins at 1, and the code continues looping until the counter variable reaches 12, at which point the code terminates.
<?php
$counter = 1;
while($counter <= 12) {
echo $counter." times 2 is ".($counter * 2)."<br />";
$counter++;
}
?>