PHP do…while loop



In the our previous two chapters we have seen for loop and while loop

PHP do…while loop :

  1. do..while loop does not test the condition before going in the loop.
  2. do..while loop is exit controlled loop, i.e condition will be checked after the execution of the body i.e at the time of exit.
  3. do…while loop is guaranteed to execute at least one time

Syntax of do..while loop :

do
{
  statement(s);
}while( condition );

Example of do..while loop :

<html>
<body>

<?php

$i = 0;

do {
   echo "Value of i : $i" . "<br />";
   $i++;
}while( $i < 10 );

?>

</body>
</html>

Output :

Value of i : 0
Value of i : 1
Value of i : 2
Value of i : 3
Value of i : 4
Value of i : 5
Value of i : 6
Value of i : 7
Value of i : 8
Value of i : 9

Flowchart of do..while loop :

cpp-do-while-loop