Difference between break and continue in PHP?

后端 未结 10 2436
粉色の甜心
粉色の甜心 2020-12-02 05:02

What is the difference between break and continue in PHP?

相关标签:
10条回答
  • 2020-12-02 05:40

    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

    0 讨论(0)
  • '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..

    0 讨论(0)
  • 2020-12-02 05:45

    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.
    
    0 讨论(0)
  • 2020-12-02 05:47

    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;

    0 讨论(0)
  • 2020-12-02 05:51

    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
    }
    
    0 讨论(0)
  • 2020-12-02 05:51

    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.

    0 讨论(0)
提交回复
热议问题