Skip to content
Home » How to break out of a foreach loop in PHP

How to break out of a foreach loop in PHP

We’ve seen loops in PHP, these are for, foreach, while and do-while. All of these loops over iterables like arrays and objects. Normally, the loop terminates when it has looped through an entire array or the number of times explicitly specified.

However, there are some instances where you might be tempted to terminate the loop immediately. This termination is what we call breaking out of a loop. In this article, we’ll see how to break out of a foreach loop in PHP. In the end, we’ll also review an example where we use could use the break keyword to terminate a loop immediately. So, let’s move on to the exciting stuff.

Break Keyword

The break keyword is used for terminating a loop immediately. It is relevant in PHP and all other programming languages. The break in PHP could be used for all kinds of loops. Here’s the syntax.

break number_of_loops_to_break;

The break in PHP alone can terminate a loop. However, for nested loops, it takes an integer argument specifying the number of loops to break. We’ll put it into use in the following example.

Break out of a foreach loop in PHP using break PHP

In this example, we will use the break keyword to terminate a foreach loop. For clarity, we’ll use an array of numbers from one to ten and break out of the loop when the loop is at, let’s say six.

<?php
 
//Numbers from one to ten.
$numbers_array = array(1,2,3,4,5,6,7,8,9,10);
 
foreach($numbers_array as $num)
{
    //If the loop is at six.
    if($num == 6)
    {
        break;
    }
 
    echo $num.PHP_EOL;
}
 
/*
OUTPUT
1
2
3
4
5
*/
?>

Just as expected, the break terminates the loop at six. Quite easy! Let’s see another example of nested foreach loops.

Break out of a nested foreach loop in PHP using break PHP

Loops within loops are nested loops. So, using a break inside a nested loop will terminate it only while the outer loop would still be intact. What if we want to terminate all the loops in one shot. That’s what we’re going to see in this example.

<?php
 
//Numbers from one to ten. Three dimensional array.
$numbers_array = array(1,2,3,array(4,5,6,array(7,8,9,10)));
 
//Loop through the first level array.
foreach($numbers_array as $outer)
{
    //If a second level array instead of a number, use another loop.
    if(is_array($outer))
    {
        foreach($outer as $inner)
        {
            //If third level array inside the second level array
            if(is_array($inner))
            {
                
                foreach($inner as $inner_most)
                {
                    if($inner_most == 8)
                    {
                        break 3;
                    }
 
                    echo $inner_most.PHP_EOL;
                }
 
            }
 
            echo $inner.PHP_EOL;
        }
    }
 
    echo $outer.PHP_EOL;
 
 
}
 
/*
OUTPUT
1
2
3
4
5
6
7
*/
?>

We have a similar array but with three levels of arrays, that is, an array having an array which in turn have another array. So, we require three nested loops to loop through this structure. You can see that we’ve introduced the break keyword appended with 3 in the innermost loop.

This 3 signifies that the program needs to terminate all of the loops at once. Contrary to this, a simple break would have just terminated the third loop and moved on to the second loop. However, with this approach, we deal with all the loops at once.

Practical Example

In this example, the code loops through an array and stops at a number that is divisible by 5, thus terminating the loop.

<?php
$numbers = array(2,8,19,22,32,36,77,75,1,43,37);
 
foreach($numbers as $num)
{
    if($num % 5 == 0)
    {
        echo $num." is divisible by 5".PHP_EOL;
        break;
    }
    echo $num." is not divisible by 5".PHP_EOL;
    
}
 
/*
OUTPUT
2 is not divisible by 5 
8 is not divisible by 5 
19 is not divisible by 5
22 is not divisible by 5
32 is not divisible by 5
36 is not divisible by 5
77 is not divisible by 5
75 is divisible by 5  
*/
?>

So, the program breaks at number 75 as it is divisible by 5. The program terminates on the spot and prevents the loop to continue beyond 75. This is possible with break in PHP.

Conclusion

In this brief article, we’ve seen how to break out of a foreach loop using break in PHP. We’ve also seen how to break out of nested loops at once by appending an integer argument. Finally, we reviewed a program where the break could be used. So, that’s all about this article. See you soon in another interesting PHP article.