Break Statement in PHP

The break statement in PHP is a control flow statement used to exit the current loop iteration or stop the entire loop prematurely.

You can use the break statement to control the flow of execution within loops such as for, foreach, while, do-while and switch control structure.

Whenever a break statement is encountered within the loop body or switch statement, it terminates the execution of a loop or switch statement prematurely and the control is transferred to the next statement immediately following the loop or switch statement.

The break statement is often used to:

  • Exit loops entirely when the specified condition is met.
  • Stop execution of a switch statement once a matching case is found.
  • Improve program efficiency by avoiding unnecessary iterations.

Syntax of Break Statement in PHP


The general syntax to use the break statement in PHP is as follows:

<?php
// Jump statement.
break [level];
?>

In the above syntax of break statement, break is a keyword. The parameter level is optional which specifies the number of nested loops to break out of. The default value is 1, meaning that it exits the innermost loop or switch. However, without a label name, you can only use break inside a loop or a switch.

Using Break in Loops


The break statement is commonly used in for, while, and do-while loops in PHP. You can use a break inside the body of loop to come out of it. When a break statement executes inside a loop body, the loop immediately ends at a specified condition.

As a result, the control continues the execution of the next statement immediately following the loop body. Here is some practical examples based on it.

Example 1: Break in for loop

<?php
// Using for loop.
for($i = 1; $i <= 10; $i++)
{
   if($i == 5) {
       break; // will stop the entirely when $i is 5.
   }
   echo "I = " . $i . "\n";
}
?>
Output:
      I = 1
      I = 2
      I = 3
      I = 4

In this example, when $i becomes 5, the break statement executes and terminates the current loop and the remaining iterations are skipped.


Example 2: Break statement in while loop

<?php
$i = 1;
while ($i <= 10) {
   if ($i == 4) {
      break; // exits the loop when $i is 4.
   }
   echo "Count: $i \n";
   $i++;
}
?>
Output:
      Count: 1 
      Count: 2 
      Count: 3 

Example 3: Break statement in do-while loop

<?php
$i = 1;
do {
  echo "Number: $i \n";
  if ($i == 5) {
     break;
  }
  $i++;
} while ($i <= 10);
?>
Output:
     Number: 1 
     Number: 2 
     Number: 3 
     Number: 4 
     Number: 5

Using Break Statement in Switch Case


You can also use the break statement to terminate the execution of a case inside the switch block and stop the execution of more cases inside the switch block. Without break statement, the execution inside the switch block continues into subsequent cases (called “fall-through”).


Example 4:

<?php
$day = 3;

switch ($day) {
   case 1:
      echo "Monday";
      break;
   case 2:
      echo "Tuesday";
      break;
   case 3:
      echo "Wednesday";
      break;
   default:
      echo "Invalid day";
      break;
}
?>
Output:
      Wednesday

Example 5: Without break in switch

<?php
$day = 2;

switch ($day) {
   case 1:
      echo "Monday";
   case 2:
      echo "Tuesday";
   case 3:
      echo "Wednesday";
   default:
      echo "Invalid day";
}
?>
Output:
      TuesdayWednesdayInvalid day

Nested Loops with Break


When loops are nested in PHP, the break statement by default only exits the innermost loop in which it is placed. The outer loop continues execution unless explicitly instructed to terminate using a higher break level.

Example 6:

<?php
for ($i = 1; $i <= 3; $i++) {
  for ($j = 1; $j <= 3; $j++) {
     if ($j == 2) {
        break; // Breaks only the inner loop
     }
    echo "i = $i, j = $j \n";
  }
}
?>
Output:
      i = 1, j = 1 
      i = 2, j = 1 
      i = 3, j = 1 

How to Exit Both Loops?


To break out of multiple levels of nested loops, you can specify the break level.

Example 7:

<?php
for ($i = 1; $i <= 3; $i++) {
   for ($j = 1; $j <= 3; $j++) {
     if ($j == 2) {
     // Break statement with label.
        break 2; // Breaks both the inner and outer loops
     }
     echo "i = $i, j = $j \n";
   }
}
?>
Output:
      i = 1, j = 1

In this example, the statement break 2 exits both inner and outer loops when $j == 2.

Key Points:

  • Default Behavior: breaks only the innermost loop in which it is placed.
  • To Exit More Levels: Use break N to exit multiple levels of nesting, where N is the number of loops to break out of.

In this tutorial, you have learned about how to use break statement in PHP to control the flow of execution inside the loops and switch block. I hope that you will have understood the basic concepts of break statement.