PHP Break and Continue

This article will help you understand PHP break and continue and how they can be implemented to improve performance and optimize code.

PHP break and continue statements helps control program flow. When you use break statement, you can exit a loop instantly, while the continue statement skips certain iterations.

You can use these statements to quickly exit loops or skip certain iterations depending on specific conditions.



PHP Break Statement

Using PHP break statement, you can exit a loop/switch statement immediately.

It is useful to use a break statement when it is needed to stop the execution of a loop or switch statement before all its iterations are complete.

Whenever the break statement is encountered within a loop, it immediately exits the loop and continues execution outside the loop. Similarly, when PHP break statement is encountered inside a switch statement, it exits the switch statement and moves on to the next statement outside of the switch statement.

In the example below, If a is equal to 3, the loop terminates:

Example: 

<?php for ($a = 0; $a < 5; $a++) { if ($a == 3) { break; } echo "$a <br>"; } ?>
In the example below, If flower[$a] is equal to “tulip“, the loop terminates:

Example: 

<?php $flower = array("sunflower", "rose", "tulip", "jasmine"); for ($a = 0; $a < count($flower); $a++) { if ($flower[$a] == "tulip") { break; } echo $flower[$a]." <br>"; } ?>

PHP Break and Continue


PHP Continue Statement

PHP continue statement skips the current iteration of a loop and moves on to the next.
It is useful to use the continue statement when there is a requirement for skipping certain elements in a loop that do not meet certain criteria.
When the continue statement appears inside a loop, it skips the current iteration of the loop and moves on to the next one.
The loop will continue until all iterations have been completed or until a break statement is encountered.
In the example below, if a is equal to 3, the loop skips 3 and continues to the next:

Example: 

<?php for ($a = 0; $a < 5; $a++) { if ($a == 3) { continue; } echo "$a <br>"; } ?>
In the example below, If flower[$a] is equal to “tulip”, the loop skips “tulip” and continues to the next:

Example: 

<?php $flower = array("sunflower", "rose", "tulip", "jasmine"); for ($a = 0; $a < count($flower); $a++) { if ($flower[$a] == "tulip") { continue; } echo $flower[$a]." <br>"; } ?>

Break and Continue with While Loop

We can also use break and continue statements in a while loop.

In the following examples, we will see the use of the break and continue statement in the while loop is the same as used in the for loop/switch statement.

The following example terminates the while loop if $a is equal to 3:

Example: 

<?php $a = 0; while($a < 5) { if ($a == 3) { break; } echo "$a <br>"; $a++; } ?>
Here in the example below, if flower[$a] is equal to “tulip”, the while loop terminates:

Example: 

<?php $a = 0; $flower = array("sunflower", "rose", "tulip", "jasmine"); while($a < count($flower)) { if ($flower[$a] == "tulip") { break; } echo $flower[$a]."<br>"; $a++; } ?>
The following example skips the loop iteration if $a is equal to 3 and continues to the next:

Example: 

<?php $a = 0; while($a < 5) { if ($a == 3) { $a++; continue; } echo "$a <br>"; $a++; } ?>
Here in the example below, if flower[$a] is equal to “tulip”, the while loop skips “tulip” and continues to the next:

Example: 

<?php $a = 0; $flower = array("sunflower", "rose", "tulip", "jasmine"); while($a < count($flower)) { if ($flower[$a] == "tulip") { $a++; continue; } echo $flower[$a]."<br>"; $a++; } ?>

Example Explanation

Above example creates an array of flowers called $flower and uses a while loop to iterate through the array.

The variable $a is initialized to 0, and is incremented by 1 during each iteration of the loop.

Inside the loop, an if statement is used to check if the current flower is a “tulip“. If it is, the “continue” statement is executed, which skips over the current iteration of the loop and moves on to the next iteration.

This means that the “tulip” flower will not be printed out.

If the current flower is not a “tulip”, the code proceeds to the next line which uses the “echostatement to print the name of the current flower on a new line, with an HTML <br> tag. The output of this code would be a list of all the flowers in the $flower array, except for the “tulip” flower.

You now know how to break loops using PHP break and continue statements.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *