do-while statements are similar to While statements, except that the condition is tested at the end of each iteration, rather than at the beginning. This means that the Do...While the loop is guaranteed to run at least once.
do
{
code to be exected;
}
while (condition);
Do While Loop Working Steps:
The example below first sets a variable $val to 1 ($val = 1). Then, the do-while loop will write some output, and then increment the variable $val with 1. Then the condition is checked (is $val less than, or equal to 3?), and the loop will continue to run as long as $val is less than, or equal to 3:
<?php
$val=1;
do {
echo "Number : $val<br />";
echo "Php <br />";
$val++;
}while ($val<=3)
?>
Output :
Number 1
Php
Number 2
Php
Number 3
Php
Note: do-while loop the condition is tested AFTER executing the statements within the loop. This means that the do-while loop would execute its statements at least once, even if the condition is false the first time.
Php Do While Loop Example 2
The example below sets the $val variable to 5, then it runs the loop, and then the condition is checked:
<?php
$val = 5;
do {
echo "My Number is: $val <br>";
$val++;
} while ($val <= 4);
?>
Output :
My Number is 5
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning