186

What is the difference between break and continue in PHP?

powtac
  • 40,542
  • 28
  • 115
  • 170

10 Answers10

551

break ends a loop completely, continue just shortcuts the current iteration and moves on to the next iteration.

while ($foo) {   <--------------------┐
    continue;    --- goes back here --┘
    break;       ----- jumps here ----┐
}                                     |
                 <--------------------┘

This would be used like so:

while ($droid = searchDroids()) {
    if ($droid != $theDroidYoureLookingFor) {
        continue; // ..the search with the next droid
    }

    $foundDroidYoureLookingFor = true;
    break; // ..off the search
}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 57
    Misuse of these functions results in this: http://www.flickr.com/photos/24973901@N04/2762458387/ – designosis Sep 13 '12 at 11:02
  • 7
    Love this answer! Remind's me of WP.org's recommendation on Yoda Conditions: http://make.wordpress.org/core/handbook/coding-standards/php/#yoda-conditions – Bob Gregor Nov 11 '13 at 17:32
  • 4
    it's 7 years after this answer but it worth to say this. as in [php documents](http://php.net/manual/en/control-structures.continue.php) from v4 `break` and `continue` are same in `switch`. both exit from switch. to exit from outer loop if there is for or so use `continue 2`. – Amin.Qarabaqi Dec 11 '17 at 07:24
  • @BobGregor Currently that part can be found at https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#yoda-conditions – Douwe de Haan Nov 06 '19 at 15:23
51

break exits the loop you are in, continue starts with the next cycle of the loop immediatly.

Example:

$i = 10;
while (--$i)
{
    if ($i == 8)
    {
        continue;
    }
    if ($i == 5)
    {
        break;
    }
    echo $i . "\n";
}

will output:

9
7
6
Hinek
  • 9,519
  • 12
  • 52
  • 74
19

BREAK:

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

CONTINUE:

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

So depending on your need, you can reset the position currently being executed in your code to a different level of the current nesting.

Also, see here for an artical detailing Break vs Continue with a number of examples

SW4
  • 69,876
  • 20
  • 132
  • 137
16

For the Record:

Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

Igor Parra
  • 10,214
  • 10
  • 69
  • 101
7

break used to get out from the loop statement, but continue just stop script on specific condition and then continue looping statement until reach the end..

for($i=0; $i<10; $i++){
    if($i == 5){
        echo "It reach five<br>";
        continue;
    }
    echo $i . "<br>";
}

echo "<hr>";

for($i=0; $i<10; $i++){
    if($i == 5){
         echo "It reach end<br>";
         break;
    }
    echo $i . "<br>";
}

Hope it can help u;

Joko Wandiro
  • 1,957
  • 1
  • 18
  • 28
5

'continue' is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

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

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

Check out the following links:

http://www.php.net/manual/en/control-structures.break.php

http://www.php.net/manual/en/control-structures.continue.php

Hope it helps..

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
4

Break ends the current loop/control structure and skips to the end of it, no matter how many more times the loop otherwise would have repeated.

Continue skips to the beginning of the next iteration of the loop.

DGH
  • 11,189
  • 2
  • 23
  • 24
3

break will stop the current loop (or pass an integer to tell it how many loops to break from).

continue will stop the current iteration and start the next one.

alex
  • 479,566
  • 201
  • 878
  • 984
2

break will exit the loop, while continue will start the next cycle of the loop immediately.

cspolton
  • 4,495
  • 4
  • 26
  • 34
2

I am not writing anything same here. Just a changelog note from PHP manual.


Changelog for continue

Version Description

7.0.0 - continue outside of a loop or switch control structure is now detected at compile-time instead of run-time as before, and triggers an E_COMPILE_ERROR.

5.4.0   continue 0; is no longer valid. In previous versions it was interpreted the same as continue 1;.

5.4.0   Removed the ability to pass in variables (e.g., $num = 2; continue $num;) as the numerical argument.

Changelog for break

Version Description

7.0.0   break outside of a loop or switch control structure is now detected at compile-time instead of run-time as before, and triggers an E_COMPILE_ERROR.

5.4.0   break 0; is no longer valid. In previous versions it was interpreted the same as break 1;.

5.4.0   Removed the ability to pass in variables (e.g., $num = 2; break $num;) as the numerical argument.