The PHP break used to log out or ending the PHP loop immediately, this expression can be used in for loop, foreach, while, do while and switch.

The expression of loop break would be like the following code.

break;

The PHP break accepts a numerical arguments which allow the nested loop to exit it directly. Let’s see how it works.

How Does PHP Break Work

Sometimes, and during the iteration you may need to exit the loop after getting some information for the compound data.

PHP Break

In this figure the loop will exit once it reached the true in IF condition block.

Let’s take some real examples with PHP break statement.

Using PHP Break with For Loop

In the following example the loop will stop completely once it reaches the loop number 3.

<?php 
  for( $i = 0; $i < 3; $i++ ) {
     echo "Loop Number {$i} \t"; 
     if ( $i == 2 ) {
       break;
     }
  }
?>

The output:

Loop Number 0
Loop Number 1
Loop Number 2

Using PHP Break with Foreach Loop

In the next example I will use the associative array and end the loop once it reaches a specific key in the array.

<?php 

   $names = array( 
     -1     => "Tarek", 
     "data" => "Hassan", 
     false  => "Khaled", 
    "md"     => "Peter" 
  );
  
  foreach( $names as $key => $value ){
     
     if ( ! (bool) $key ) {
       break;
     }
     
     echo $value . "\t";
     
  }

?>

The output:

Tarek Hassan

Using PHP Break with While and Do While Loop

As you know, the while is a loop doing iteration until it reaches to the false value in the condition block part.

To terminate the loop in while you have to use break statement. And that can be done like in the following example.

<?php 
  $xz = 0;
  while ( true ) {
    echo "Loop number {$xz} \n";

    if ( $xz == 3 ) {
      break;
    }

    $xz++;
  }
?>

The output:

Loop number 0 
Loop number 1 
Loop number 2 
Loop number 3

Using PHP Break with Switch

The break in switch means to execute the current statements then prevent the other pieces of code from the execution and exit the switch from the condition.

Let’s see an example.

<?php 
  $yz = 5;
  switch( $yz ) {

    case 1:
      echo "Executed with {$yz} number !";
      break;

    case 5:
      echo "Executed with {$yz} number !";
      break; // => Exit the switch statement here
                
    case 3:
      echo "Executed with {$yz} number !";
      break;

  }
?>

The output:

Executed with 5 number !

But if you didn’t use the break after every case in switch that means it will execute the all cases after the correct statement.

So try to remove the break statement from all cases.

<?php 
  $yz = 5;
  switch( $yz ) {

    case 1: // => will not execute this block 
      echo "Executed with {$yz} number !";
      

    case 5: // => Execute this
      echo "Executed with {$yz} number ! \n";
      
                 
    case 3: // => Execute this
      echo "Executed with {$yz} number ! \n";
       

  }
?>

The output:

Executed with 5 number !
Executed with 5 number !

How to Exit the Nested Loops?

As I mentioned before, you can use arguments in break word during the loop to exit it. But if the argument has 2 and you only using one loop that will show you an error like the following.

<?php

  for( $i = 0; $i < 5; $i++ ){
    if ( $i == 3 ) {
      break 2;
    }
  }

?>

The output:

PHP Fatal error: Cannot 'break' 2 levels in index.php on line 10

That is happening because I used only one loop, it was not nested loop. Lets try the same example with the nested loop.

<?php

  for($x = 0; $x < 5; $x++ ) {
    echo "Loop 1 index $x \n";
    for( $i = 0; $i < 5; $i++ ){
      if ( $i == 3 ) {
        echo "Loop 2 index $i \n";
        break 2;
      }
    }
  }

?>

The output:

Loop 1 index 0 
Loop 2 index 3

Wrapping Up

In this tutorial you understood how to use the PHP break statement with all PHP loops and that was with examples.