Continue or Break

break Statement in PHP?

The break statement in PHP is used to terminate the current flow of the program. The break statement is mostly used to break the execution of the for, while, do…while, foreach and switch statements on the basis of specified conditions.

The break statement can also be used to jump out of a loop.

 

Example:

<?php
for ($i = 0; $i < 10; $i++) 
{
  if ($i == 4) {
    break;
  }
  echo "The number is: $i <br>";
}
?>

This example jumps out of the loop when i is equal to 4:

 

Output:

The number is: 0
The number is: 1
The number is: 2
The number is: 3

 

continue Statement in PHP?

The continue statement in the PHP is used to iterate the loop by skipping the current flow when the condition is satisfied. When we want to go to the next iteration of a loop or switch control structure, we use the continue statement. The continue statement is mostly used to break the execution of the for, while, do…while foreach and switch statements on the basis of a specified condition.

 

Example: 

<?php  
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}
?>

This example skips the value of 4:

 

Output:

The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9