Difference between break and continue in PHP?

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

What is the difference between break and continue in PHP?

相关标签:
10条回答
  • 2020-12-02 06:01

    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.

    0 讨论(0)
  • 2020-12-02 06:03

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

    0 讨论(0)
  • 2020-12-02 06:06

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

    For the Record:

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

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