Updated March 21, 2023
Introduction to Break Statement in PHP
PHP break statement is used to exit from a loop instantaneously without having to wait for getting back at the conditional statements like for loop, while loop, do-while, switch and for-each loop. If there are multiple loops present and the break statement is used, it exits only from the first inner loop. Break is present inside the statement block and gives the user full freedom to come out of the loop whenever required.
Syntax:
<?php
//variable declarations
//conditional statement block
{
break;
}
?>
Flowchart:
As shown above, the code first enters the conditional statement block once the condition for the loop is satisfied and continuously executes the statements in the loop until the condition is not satisfied. When a break statement is written in the code, as soon as the program encounters it the code exits from the current loop irrespective of whether the condition is satisfied or not as shown.
Examples of Break in PHP
Let us understand the working of a break statement by taking a few examples for each conditional statement in various scenarios and checking its behavior for the same.
Example #1
Break statement inside the “for” loop.
Code:
<?php
$var = 1;
for ($var = 0;$var <= 10;$var++)
{
if ($var==5)
{
break;
}
echo $var;
echo "\n";
}
echo "For loop ends here" ;
?>
Output:
Here we are printing the numbers from 1 to 10 in the for loop by initializing 1 to variable “var”. “var” starts printing incremental numbers starting from 1 till it encounters the if loop condition. Here we are mentioning that the variable should come out of the loop once its value reaches 5. This is done using the break statement as shown. We can see the same in the output as we are printing “For loop ends here” once the break statement is executed and it comes out of for loop even if for loop condition is not satisfied. Hence the break statement comes out of the entire logic of all other iterations.
Example #2
This example is to check the functionality of the break statement in a while loop.
Code:
<?php
$var = 0;
while( $var < 10) {
$var++;
if( $var == 5 )break;
echo ("Current variable value = $var");
echo "\n";
}
echo ("Exited loop at variable value = $var" );
?>
Output:
In the above program variable “var” is first initialized to 0 and using while loop we are incrementing its value by one and printing the same. We are writing an if condition wherein we are making the code to exit by using a break statement once the variable value equals 5. This break makes it exit from the current while loop even though the specified condition of incrementing variable until the value 10 is not met. We are displaying the variable value at which the loop is broken.
Example #3
Here we are implementing break statements in the foreach loop.
Code:
<?php
$array = array('A', 'B', 'C', 'D', 'E', 'F');
foreach ($array as $let) {
if ($let == 'E') {
break;
}
echo "$let \n";
}
Output:
In this program, we are first declaring an array containing a collection of letters. Then by using a foreach loop, we are printing all the elements of the array one by one. An if the conditional statement is introduced to break the loop once the value of array pointer reaches the letter “E”. Hence on encountering break statement the code exits without printing the next letter in the array i.e. “F”.
Example #4
The most common application of break is in a switch statement which can be shown below.
Code:
<?php
$a=1;
switch ($a) {
case 0:
echo "a equals 0";
break;
case 1:
echo "a equals 1";
break;
case 2:
echo "a equals 2";
break;
}
?>
Output:
This is the example of a simple switch statement where we are initializing the variable value to 1 at first. Then by the use of switch conditions, we are checking the variable value and printing it once the condition matches.
Example #5
Here let us see the working of a break statement when there are two or more loops (conditional statements).
Code:
<?php
// PHP program to verify break of inner loop
// Declaration of 2 arrays as below
$array1 = array( 'One', 'Two', 'Three' );
$array2 = array( 'Three', 'One', 'Two', 'Four' );
// Outer foreach loop
foreach ($array1 as $a1) {
echo "$a1 ";
// Inner nested foreach loop
foreach ($array2 as $a2) {
if ($a1 != $a2 )
echo "$a2 ";
else
break 2;
}
echo "\n";
}
echo "\n Loop Terminated";
?>
Output:
Here we are using 2 nested foreach loops and also showing a case of using “break 2” which breaks out of both the loops in contrast to the “break” statement which breaks out of only the inner loop.
We have declared two arrays array1 and array2 and we are displaying the values of array2 for each value of array1 until and unless the value of array1 is not equal to array2. Once the value in array1 becomes the same as array2 we are breaking both loops by making use of break 2 statement which prevents the code from executing any more statements.
Example #6
Here we shall see how to use break statements to come out of “n” number of loops (conditional statements).
Code:
<?php
## Outermost first for loop
for($a=1;$a<5;$a++){
echo ("Value of a is = $a");
echo "\n";
## Second for loop
for($b=1;$b<3;$b++){
echo ("Value of b is = $b");
echo "\n";
## Third for loop
for($c=2;$c<3;$c++){
echo ("Value of c is = $c");
echo "\n";
## Fourth for loop
for($d=2;$d<4;$d++){
echo ("Value of d is = $d");
echo "\n";
if( $a == 3 ){
break 4;
}
}
}
}
}
echo 'Loop has terminated and value of a = '.$a;
?>
Output:
The break statement followed by the number of loops that need to be exited from are used to terminate from “n” number of loops.
Syntax:
break n;
where n is the number of loops that need to be exited from the loop.
We are using 4 nested for loops for this purpose. Variables a, b, c, d is initialized respectively for each for loop and we are incrementing their values by 1. The same values are displayed in the output to understand its flow better. At last, we are giving a condition for all the 4 loops to break if the value of the first variable becomes equal to 3 which it eventually did and can be shown in the output. We are printing the loop has terminated at the end along with as value to note the break’s functionality.
Conclusion
When we are using one or more conditional statements in our code and we need to exit from the same at a particular point, we can use the break statement. Basically, it helps to terminate from the code when the condition we give falls TRUE. We can also pass an integer along with break to terminate from any number of existing loops instead of declaring the break again and again.
Recommended Articles
This is a guide to Break in PHP. Here we discuss the introduction and top 6 examples of break in PHP using different conditions along with its implementation. You may also look at the following articles to learn more-