Difference between break and continue in PHP?

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

What is the difference between break and continue in PHP?

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
    }
    

提交回复
热议问题