The while loop is the simple loop that executes nested statements repeatedly while the expression value is true. The expression is checked every time at the beginning of the loop, and if the expression evaluates to true then the loop is executed otherwise loop is terminated.
Flowchart of While Loop:
Syntax:
while (if the condition is true) {
// Code is executed
}
Example 1: This example uses a while loop to display numbers.
PHP
<?php
$num = 10;
while ( $num < 20) {
echo $num . "\n" ;
$num += 2;
}
?>
|
while-endWhile loop:
Syntax:
while (if the condition is true):
// Code is executed
...
endwhile;
Example 2: This example uses while and endwhile to display the numbers.
PHP
<?php
$num = 10;
while ( $num < 20):
echo $num . "\n" ;
$num += 2;
endwhile ;
?>
|
Reference: https://www.php.net/manual/en/control-structures.while.php
Last Updated :
22 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...