PHP: While loop

While loops are the simplest type of loop in PHP.

The basic form of a while statement is:

while (expression)

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.

$i = 1;
while ($i <= 10) {
    echo $i;
    $i = $i + 1;  
    echo "\n";

There are 3 statements in the while loop.  The condition is while $i <= 10, the 3 statements will be ran.

$i is increased by 1 each time the loop is ran and the condition will be checked after it is increased by 1.

Series Navigation<< PHP: elseif statement
PHP: For loop >>