PHP Tutorial Tutorials - PHP while Loop

PHP while Loop

The while statement will execute the nested statement(s) repeatedly as long as the condition specified in the while statement evaluate to true.

while (condition){
   // statement to be executed repeatedly
   // as long as the condition is TRUE.
}

The following examples print the numbers 1 through 10. The loop will continue to run as long as $i is less than or equal to 10. The $i will increase by 1 each time the loop runs:

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

Alternative syntax for while loop statement

In while loop statement, We can change the opening brace to a colon: and the closing brace to endwhile;

<?php
$i = 1;
while ($i <= 10):
    echo $i;
    $i++;
endwhile;
?>
Date:2019-10-01 02:33:36 From:www.Lautturi.com author:Lautturi