Difference between break and continue in PHP?

Php

Php Problem Overview


What is the difference between break and continue in PHP?

Php Solutions


Solution 1 - Php

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
}

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

For the Record:

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

Solution 5 - Php

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;

Solution 6 - Php

'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..

Solution 7 - Php

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.

Solution 8 - Php

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.

Solution 9 - Php

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

Solution 10 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpowtacView Question on Stackoverflow
Solution 1 - PhpdecezeView Answer on Stackoverflow
Solution 2 - PhpHinekView Answer on Stackoverflow
Solution 3 - PhpSW4View Answer on Stackoverflow
Solution 4 - PhpIgor ParraView Answer on Stackoverflow
Solution 5 - PhpJoko WandiroView Answer on Stackoverflow
Solution 6 - PhpMahendra LiyaView Answer on Stackoverflow
Solution 7 - PhpDGHView Answer on Stackoverflow
Solution 8 - PhpalexView Answer on Stackoverflow
Solution 9 - PhpcspoltonView Answer on Stackoverflow
Solution 10 - PhpMuhammed Shihabudeen Labba AView Answer on Stackoverflow