PHP Break and Continue


August 23, 2021, Learn eTutorial
2089

In this PHP tutorial, you will learn all about conditional statements in PHP. We will discuss in detail the break and continue statements.

What is a break Statement in PHP?

The break statement in PHP is used to terminate the current flow of the program. The break statement is mostly used to break the execution of the for, while, do…while, foreach and switch statements on the basis of specified conditions.

Syntax


jump statement;  
break;

PHP - Break statement

Using the break statement in the loop

While using the break statement inside the loop it will terminate the execution of the loop when it meets the condition of the break statement even if doesn’t satisfy the loop condition.

Example :break Statement in PHP


for ($i = 1; $i <= 10; $i++) {    
echo "$i ";    
if ($i == 3) {    
break;    
}    
}    
 
 

Output:


1 2 3

In the above example we can see that the loop condition is to iterate the value of the variable “i” from 1 to 10. And in the block of the loop, we are printing the value of the variable “i” and we are checking the condition “i == 3” and in the block of the if statement we are using the break statement. According to it if the condition is met the loop should be terminated. And we can see that the loop should iterate till the value of the variable “i” becomes “10” and by using the break statement it doesn’t satisfy that and exit the loop when the break condition is met.

Using the break statement in the inner loop

Similarly, when we use the break statement inside the inner loop it will only terminate the inner loop and it won’t affect the outer loop.

Example :break Statement in PHP


for ($i = 1; $i <= 3; $i++) {    
    for ($j = 1; $j <= 5; $j++) {
        echo "$i * $j = ". $i * $j. "\n";
        if ($j == 3) {
            break;
        }
    }    
}    

 

Output:


1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9

In the above example we can see that the loop condition is to iterate the value of the variable “i” from 1 to 3 and likewise the inner loop should iterate the value of the variable “j” from 1 to 5 in the block of the loop, we are printing the value of the multiplied value of the variables “i” and “j” and according the loop condition the outer loop should iterate for 3 times and the inner loop should iterate for 5 times but in the above case we can see that in the block of the inner loop we have the condition “j == 3” and in the block of that condition break statement is used and by that the inner loop won't be iterating 5 times and we can also see that the beak statement is not affecting the outer loop and the outer loop completes its whole iteration.

Using break statement in the switch statement

Even though the break statement is optional in the switch statement it is mostly recommended to use it. If we are not using the break statement in the switch statement even if met the conditions it will further check the whole condition. And in the other case if we use the break statement when the condition is met it will directly quit the switch statement even if there are more conditions.

Example of using switch statement without break statementP


$ch = 3;  
    switch ($ch)  
    {     
        case 1:   
            echo "Monday ";  
        case 2:   
            echo "Tuesday ";  
        case 3:   
            echo "Wednesday ";  
        case 4:   
            echo "Thursday ";  
        case 5:   
            echo "Friday ";  
        case 6:   
            echo "Saturday ";  
        case 7:   
            echo "Sunday ";  
        default:   
            echo "Wrong Choice ";  
    }  
 
 

Output:


Wednesday Thursday Friday Saturday Sunday Wrong Choice

Example of using switch statement with break statement


<?php
$ch = 3;  
    switch ($ch)  
    {     
        case 1:   
            echo "Monday";  
            break;  
        case 2:   
            echo "Tuesday";  
            break;  
        case 3:   
            echo "Wednesday";  
            break;  
        case 4:   
            echo "Thursday";  
            break; 
        case 5:   
            echo "Friday";  
            break;  
        case 6:   
            echo "Saturday";  
            break;  
        case 7:   
            echo "Sunday";  
            break; 
        default:   
            echo "Wrong Choice";  
            break;  
    }  

 

Output:


Wednesday

The above examples are to print the day of the week and we can see that when the break statement is not used every case after the condition is met will be executed and when the break statement is used it will only execute the case where the condition becomes true.

What is continue Statement in PHP?

The continue statement in the PHP is used to iterate the loop by skipping the current flow when the condition is satisfied. When we want to go to the next iteration of a loop or switch control structure, we use the continue statement. The continue statement is mostly used to break the execution of the for, while, do…while foreach and switch statements on the basis of a specified condition.

Syntax


jump-statement;  
continue;  

PHP - Continue statement

Using the continue statement in the loop

While using the continue statement inside the loop it will skip the execution of the current iteration when it meets the condition of the continue statement and continues the remaining flow of the loop.


<?php
for ($i = 1; $i <= 10; $i++) {    
if ($i == 3) {    
    continue;    
}
echo "$i ";
}  

 

Output:


1 2 4 5 6 7 8 9 10

In the above example, we can see that the loop condition is to iterate the value of the variable “i” from 1 to 10. And in the block, we are checking the condition “i == 3” and it becomes we use the continue statement and after the condition block, we are printing the values of the variable “i”. By using the continue statement we skip the current flow of the iteration and perform the remaining iteration of the loop. As we said we can see that in the output it skips the “3” in the output while printing the values.

Using the continue statement in the inner loop

Similarly, when we use the continue statement inside the inner loop it will only skip the remaining code and perform the next iteration.


for ($i = 1; $i <= 3; $i++) {    
    for ($j = 1; $j <= 3; $j++) {
        if ($i == $j) {
            continue;
        }
        echo "$i $j \n";
    }    
}    
 

Output:


1 2 
1 3 
2 1 
2 3 
3 1 
3 2

In the above example, we can see that the loop condition is to iterate the value of the variable “i” from 1 to 3 and likewise the inner loop should iterate the value of the variable “j” from 1 to 3 in the block of the loop, we are printing the value of the variables “i” and “j” and according to the loop condition the outer loop should iterate for 3 times and the inner loop should iterate for 3 times but in the above case we can see that in the block of the inner loop we have the condition “i == j” and in the block of that condition continue statement is used and by that the program will skips the printing when the value of the variable “i” and “j” becomes equal.