while loops are the simplest type of loop in PHP. They behave just like their C counterparts.
while (expression) {
//statement;
}
The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE
.
The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration).
Sometimes, if the while expression evaluates to FALSE
from the very beginning, the nested statement(s) won't even be run once.
Simplest example of while loop for print 1 to 10.
<?php
$i = 1;
while ($i <= 10) {
echo $i."<br />";
$i++;
}
?>
Ask Question