The break statement in PHP is utilized to terminate the execution of the current loop or switch case statement. However, when dealing with nested loops and the intention is to exit from one or more of the outer loops, you can achieve this by providing a numeric value with the break statement.
This tutorial demonstrates the process of terminating a specified number of nested foreach, for, while, and do-while loops in PHP by utilizing the break statement.
Table of Content
- Break 2 foreach, for, while, and do-while loops
- Break n number of nested foreach, for, while, and do-while loops
- Conclusion
1. Break 2 foreach, for, while, and do-while loops
The break statement takes an optional numeric value its default value is 1
. This represents the number of loops to terminate.
For out from two inner loops pass 2 with a break statement from your inner loop when the particular condition is TRUE.
Syntax –
break 2;
Break 2 foreach loop Example –
In the example, read $users
Array data using 2 foreach loop. To exit from 2 foreach loop use break 2 if mentioned condition returns true.
<?php $users[] = array("name"=>"Yogesh", "age"=>30, "gender"=>"Male"); $users[] = array("name"=>"Sonarika","age"=> 29, "gender"=>"Female"); $users[] = array("name"=>"Anil", "age"=>19, "gender"=>"Male"); $users[] = array("name"=>"Vishal", "age"=>21, "gender"=>"Male"); foreach ($users as $user) { foreach ($user as $detail) { echo "value : ".$detail."<br>"; if($detail == 'Sonarika'){ echo "-- break foreach --"; break 2; } } }
Output:
value : Yogesh value : 30 value : Male value : Sonarika -- break foreach --
Break 2 for loop Example –
Similarly, use break 2;
statement to terminate 2 for loops.
<?php for($i=0;$i<5;$i++){ for($j=0;$j<3;$j++){ if($i == 3){ break 2; } } } echo 'i = '.$i; ?>
Output:
i = 3
Break 2 while loop Example –
<?php $num1 = 0; while ($num1 < 5){ $num2 = 0; while($num2 < 5){ echo "num1 : ".$num1.", num2 : ".$num2."<br>"; if($num1 == 2 && $num2 == 3){ echo "-- break while loop --"; break 2; } $num2++; } $num1++; }
Output:
num1 : 0, num2 : 0 num1 : 0, num2 : 1 num1 : 0, num2 : 2 num1 : 0, num2 : 3 num1 : 0, num2 : 4 num1 : 1, num2 : 0 num1 : 1, num2 : 1 num1 : 1, num2 : 2 num1 : 1, num2 : 3 num1 : 1, num2 : 4 num1 : 2, num2 : 0 num1 : 2, num2 : 1 num1 : 2, num2 : 2 num1 : 2, num2 : 3 -- break while loop --
Break 2 do-while loop Example –
<?php $num1 = 0; do { $num2 = 0; do { echo "num1 : ".$num1.", num2 : ".$num2."<br>"; if($num1 == 2 && $num2 == 3){ echo "-- break do while loop --"; break 2; } $num2++; } while ($num2 < 5); $num1++; } while ($num1 < 5);
Output:
num1 : 0, num2 : 0 num1 : 0, num2 : 1 num1 : 0, num2 : 2 num1 : 0, num2 : 3 num1 : 0, num2 : 4 num1 : 1, num2 : 0 num1 : 1, num2 : 1 num1 : 1, num2 : 2 num1 : 1, num2 : 3 num1 : 1, num2 : 4 num1 : 2, num2 : 0 num1 : 2, num2 : 1 num1 : 2, num2 : 2 num1 : 2, num2 : 3 -- break do while loop --
2. Break n number of nested foreach, for, while, and do-while loops
Same as above you can break any number of loops from your inner loop.
Syntax –
break n;
Here, n is the number of outer loops you want to break.
Break n number foreach loop Example –
In the example, using 3 foreach loop to read $usersList
Array data. To exit from the 3 foreach loop use break 3;
statement. Here, 3 is the number of loop that needs to break.
<?php $usersList = array( array( array("Sunil", 18, "Male"), array("Mayank", 24, "Male") ), array( array("Rohan", 26, "Male"), array("Aditya", 28, "Male") ), array( array("Riya", 19, "Female"), array("Shalu", 21, "Female") ) ); foreach ($usersList as $users) { foreach ($users as $user) { foreach ($user as $detail) { if($detail == 'Aditya'){ echo "<pre>"; print_r($user); echo "</pre>"; echo "-- Break 3 foreach loop --"; break 3; } } } }
Output:
Array ( [0] => Aditya [1] => 28 [2] => Male ) -- Break 3 foreach loop --
Break n number for loop Example –
In this example using 4 for loops and to break from the 4th loop use break 4;
statement.
<?php ## for Loop1 for($i=0;$i<5;$i++){ ## for Loop2 for($j=0;$j<3;$j++){ ## for Loop3 for($k=0;$k<3;$k++){ ## for Loop4 for($a=0;$a<4;$a++){ if( $i == 3 ){ break 4; } } } } } echo 'i = '.$i;
Output:
i = 3
Break n number while loop Example –
<?php // Initialize variables $i = 0; $j = 0; $k = 0; $a = 0; // Loop 1 while ($i < 5) { // Loop 2 while ($j < 3) { // Loop 3 while ($k < 3) { // Loop 4 while($a < 4){ if ($i == 3) { break 4; } $a++; } $k++; $a = 0; } $j++; $k = 0; } $i++; $j = 0; } echo 'i = ' . $i;
Output:
i = 3
Break n number do-while loop Example –
<?php // Initialize variables $i = 0; // Loop 1 do { // Initialize variables $j = 0; // Loop 2 do { // Initialize variables $k = 0; // Loop 3 do { // Initialize variables $a = 0; // Loop 4 do { if ($i == 3) { break 4; } $a++; } while ($a < 4); $k++; } while ($k < 3); $j++; } while ($j < 3); $i++; } while ($i < 5); echo 'i = ' . $i;
Output:
i = 3
3. Conclusion
If you have a nested loop in your program, you can utilize the break statement to terminate a loop when a specified condition becomes TRUE.
To terminate a specified number of loops, all that is required is to include an integer value along with the break statement.
If you found this tutorial helpful then don't forget to share.