PHP

The while statement

while loops are the simplest type of loop in PHP. They behave just like their C counterparts.

Syntax:

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.

Example:

Simplest example of while loop for print 1 to 10.

<?php
    $i = 1;
    while ($i <= 10) {
        echo $i."<br />";
        $i++;
    }
?>

Output:

Tutorialik.com
1
2
3
4
5
6
7
8
9
10



Subscribe us on Youtube

Share This Page on


Ask Question