HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP do while Loop


The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified condition is true.


The do...while loop in PHP is a control flow structure that executes a block of code at least once, followed by repeated execution as long as a specified condition evaluates to true. It differs slightly from the simpler while loop.


Example


<!DOCTYPE html>
<html>
<body>

<?php  
$i = 1;

do {
  echo $i;
  $i++;
} while ($i < 6);
?>  

</body>
</html>

Output

12345

• The break Statement

The break statement does indeed terminate the loop prematurely, but it doesn't have a direct effect on the loop's condition:

Example


Stop the loop when $i is 3:

<!DOCTYPE html>
<html>
<body>

<?php  
$i = 1;

do {
  if ($i == 3) break;
  echo $i;
  $i++;
} while ($i < 6);
?>  

</body>
</html>

Output

12

• The continue Statement

The continue statement is a control flow statement used within loop structures (for, while, do-while, and foreach) in PHP.

Example


<!DOCTYPE html>
<html>
<body>

<?php  
$i = 0;

do {
  $i++;
  if ($i == 3) continue;
  echo $i;
} while ($i < 6);
?>  

</body>
</html>

Output

12456