PHP Do While Loop

  • Hi guys, In this tutorial, we will learn do-while loop statement to execute a code block repeatedly based on a condition checked at the end of each iteration using php.
  • This loop is used when position looping is performed after executing statements within the loop. This means that do-while would execute its statements at least once, even if the condition fails for the first time itself.

Php do while It is also called an exit-controlled loop.

PHP Syntax

<?php
do{
	code to be executed;
} while (condition);
?>

PHP Example

Edit it yourself

<!DOCTYPE html>
<html>
<body>

<?php
  $i = 1;
  do{
  	echo "The number is: $i <br>";
  	$i++;
  }while($i <= 5);
?>


</body>
</html>

Output of PHP comment

Edit it yourself
  • The number is: 1
  • The number is: 2
  • The number is: 3
  • The number is: 4
  • The number is: 5