Using continue in a switch statement

后端 未结 7 507
盖世英雄少女心
盖世英雄少女心 2020-12-13 03:29

I want to jump from the middle of a switch statement, to the loop statement in the following code:

while (something = get_something())
{
    swi         


        
相关标签:
7条回答
  • 2020-12-13 04:02

    This might be a megabit to late but you can use continue 2.

    Some php builds / configs will output this warning:

    PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?

    For example:

    $i = 1;
    
    while ($i <= 10) {
        $mod = $i % 4;
        echo "\r\n out $i";
        $i++;
        switch($mod)
        {
            case 0:
                break;
            case 2:
                continue;
                break;
            default:
                continue 2;
                break;
        }
        echo " is even";
    }
    

    This will output:

    out 1
    out 2 is even
    out 3
    out 4 is even
    out 5
    out 6 is even
    out 7
    out 8 is even
    out 9
    out 10 is even
    

    Tested with PHP 5.5 and higher.

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