PHP While


while the loop is the simplest loop in PHP, its basic format is:

while(expr) {
    Statement
}
// or
while(expr):
    Statement

This syntax means that as long as the expr expression is TRUE, statement is executed until expr is FALSE, and statement represents the action or logic to be executed.

example:

$i = 1;
while($i < = 10) {
    echo $i;
    $i++;
}

The example loop outputs 1 to 10.


do-while loop

The do-while loop is very similar to a while loop, except that the do-while guarantee must be executed once, while the while may not do anything when the expression does not hold.

The do-while loop has only one syntax:

do {
    Statement
} while(expr)

example:

$i = 1;
do {
    echo $i;
    $i++;
} while($i < = 10);

This example is also output 1 to 10.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook