PHP Tutorial Tutorials - PHP do-while Loop

PHP do-while Loop

do-while loops are very similar to while loops.
do-while expression is checked at the end of each iteration.
So the nested statement(s) would be executed once.
then the condition is evaluated,the nested statement(s) would be executed repeatedly as long as the specified condition evaluates to TRUE.

structure:

do{
	//do this code;
}while ( conditional expr)

Example for do-while loops:

<?php
$i = 1;
do {
    echo "do-while loop ".$i;
}while ($i > 1);

// compare with while loop
while ($i > 1){
    echo "while loop ".$i;
}
?>

Difference Between while and do…while Loop

  • with a while loop, the condition expression is checked in the beginning of each iteration.

  • With a do-while loop, the loop will always be executed once,
    Because the condition expression is checked at the end of each iteration.

Date:2019-10-01 02:36:13 From:www.Lautturi.com author:Lautturi