Table of Contents



Introduction

Both break and continuecan be used in a loop to “break out” of the loop or current iteration.

  • break ends execution of the current for, foreach, while, do-while or switch structure.

  • continue is used to skip the rest of the current loop iteration and continue with the next iteration (provided the conditional expression evaluates to true).

The break Statement

We have already seen the break statement in action when we discussed the switch statement where a break statement is inserted at the end of each case to tell PHP to exit the switch statement.

Just as we can break out of a switch statement, we can also break out of a for loop (or any loop) using the same break keyword.

break also accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.

Example

Using the break statement in a while loop.

In this example, we will sum all integers from 1 to 10.

 1<?php
 2    $sum = 0;
 3    $i = 1;
 4    while (true) {
 5        $sum += $i;
 6        echo "+ $i = $sum <br>";
 7        if (++$i > 10) break;
 8    }
 9    echo "The final sum is: $sum <br>";
10?>
+ 1 = 1
+ 2 = 3
+ 3 = 6
+ 4 = 10
+ 5 = 15
+ 6 = 21
+ 7 = 28
+ 8 = 36
+ 9 = 45
+ 10 = 55
The final sum is: 55

Notice that the use of true as the conditional expression means that the while loop will execute forever. This makes the use of a break statement mandatory.

Example

Using the break statement with a numeric argument.

 1<?php
 2    $i = 0;
 3    while (++$i) {
 4        echo "$i <br>";
 5        switch ($i) {
 6            case 5:
 7                echo "Exit switch <br>";
 8                break 1;  /* Exit only the switch. */
 9            case 10:
10                echo "Exit switch and while loop<br>";
11                break 2;  /* Exit the switch and the while. */
12            default:
13                break;
14        }
15    }
16?>
1
2
3
4
5
Exit switch
6
7
8
9
10
Exit switch and while loop

Recall that break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. In the above example, the second break statement break 2 tells PHP to exit both the switch statement and the while loop.

Finally, let’s use the break statement and 2 for loops to list the prime numbers from 1 to 20.

Example

Using the `break` statement and `for` loops to identify prime numbers.
1<?php
2    for ($i = 1; $i <= 20; $i++) {
3        for ($j = 2; $j < $i ; $j++) {
4            if ($i % $j == 0) break; // not prime
5        }
6        if ($j==$i) echo "$i is prime.<br>";
7    }
8?>
2 is prime.
3 is prime.
5 is prime.
7 is prime.
11 is prime.
13 is prime.
17 is prime.
19 is prime.

ADVERTISEMENT



The continue Statement

  • continue is used to skip the rest of the current loop iteration and continue with the next iteration (provided the conditional expression evaluates to true).

  • continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.

Note

In PHP, the switch statement is considered a looping structure for the purposes of continue. Therefore, continue behaves like break in a switch statement. For example, if a switch is inside a loop, continue 2 will continue with the next iteration of the outer loop.

Example

Using the continue statement in a for loop.

In this example, we will sum all even numbers from 1 to 10.

1<?php
2    $sum = 0;
3    for ($i = 1; $i <= 10; $i++) {
4        if ($i % 2 != 0) continue; // odd number
5        $sum += $i;
6        echo "+ $i = $sum <br>";
7    }
8    echo "The final sum is: $sum <br>";
9?>
+ 0 = 0
+ 2 = 2
+ 4 = 6
+ 6 = 12
+ 8 = 20
+ 10 = 30
The final sum is: 30

The if... continue statement behaves like a filter to filter out all odd numbers.