50

I noticed you can indeed use the continue keyword in a switch statement, but on PHP it doesn't do what I expected.

If it fails with PHP, who knows how many other languages it fails too? If I switch between languages a lot, this can be a problem if the code doesn't behave like I expect it to behave.

Should I just avoid using continue in a switch statement then?

PHP (5.2.17) fails:

for($p = 0; $p < 8; $p++){
    switch($p){
        case 5:
            print"($p)";
            continue;
            print"*"; // just for testing...
        break;
        case 6:
            print"($p)";
            continue;
            print"*";
        break;
    }
    print"$p\r\n";
}
/*
Output:
0
1
2
3
4
(5)5
(6)6
7
*/

C++ seems to work as expected (jumps to end of for loop):

for(int p = 0; p < 8; p++){
    switch(p){
        case 5:
            cout << "(" << p << ")";
            continue;
            cout << "*"; // just for testing...
        break;
        case 6:
            cout << "(" << p << ")";
            continue;
            cout << "*";
        break;
    }
    cout << p << "\r\n";
}
/*
Output:
0
1
2
3
4
(5)(6)7
*/
Rookie
  • 4,064
  • 6
  • 54
  • 86
  • end of the loop? I wouldn't necessarily call switch a loop statement, it's an extended if-else statement in my view - and do you call continue in if-else? – Zathrus Writer Sep 10 '12 at 10:35
  • I don't think its a good idea to ask for generic advice about programming constructs that are just called the same in different langauges. Seek for advice for every single language. They are different after all, all with different good practices. So my advice would be to use continue in those languages where it works, and where it is deemed to be commonly a good thing to do. – PlasmaHH Sep 10 '12 at 10:36
  • 1
    @ZathrusWriter The question says "end of for loop", not "end of the loop". The `continue` in the question is intended to jump to the end of the `for` loop. –  Sep 10 '12 at 10:38
  • @hvd lol, I need better glasses... sorry :D – Zathrus Writer Sep 10 '12 at 10:42
  • @PlasmaHH, i do sometimes convert C++ code to PHP, or other way around... thats why its very prone for errors. I dont know any other feature in PHP which behaves differently like this one. – Rookie Sep 10 '12 at 10:42
  • 3
    @Rookie: I would say there are enough big and small differences between php and c++ that you should be careful at each and every line, and not just try to "visually translate" the source code. Imho this is just one of the many trip mines you encounter when translating; between any languages, not just php and c++. – PlasmaHH Sep 10 '12 at 10:48

7 Answers7

85

Try using continue 2 to continue to the next iteration of the loop surrounding the switch statement.

EDIT:

    $foo = 'Hello';

    for ($p = 0; $p < 8; $p++) {
         switch($p) {
             case 3:
                 if ($foo === 'Hello') {
                     echo $foo;
                     break;
                 } else {
                      continue 2;
                 }

             default:
                 echo "Sleeping...<br>";
                 continue 2;

         }

         echo "World!";
         break;
    }

//This will print: Sleeping... Sleeping... Sleeping... Hello World!

newms87
  • 834
  • 1
  • 10
  • 23
Liam
  • 19,819
  • 24
  • 83
  • 123
  • 2
    I just ran into a use case for this, where I have a for loop with a switch inside. Very useful! PHP break n / continue n syntax has saved me a lot of effort in many scenarios. If you elaborate a little more on this answer, I think this should be the accepted answer. – newms87 Jan 21 '16 at 17:24
  • 1
    this is a super exciting optional argument that i never knew about. i feel like i've only just begun to learn php again – Thomas Apr 15 '16 at 16:48
  • 2
    7 years with PHP and I'm still learning things from the basics of the PHP! unbelievable! thanks a lot! – Rafael Moni Oct 13 '16 at 18:58
  • Ty sir, this is what I was looking for. – Chad Fisher Mar 22 '18 at 20:28
58

PHP 7.3 or newer:

Using continue to break a switch statement is deprecated and will trigger a warning.

To exit a switch statement, use break.

To continue to the next iteration of a loop that's surrounding the current switch statement, use continue 2.

PHP 7.2 or older:

continue and break may be used interchangeably in PHP's switch statements.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
Aesthete
  • 18,622
  • 6
  • 36
  • 45
  • That is very weird, why is it the same, whats the point duplicating commands? – Rookie Sep 10 '12 at 10:40
  • 4
    @Rookie In C++, `continue` and `break` are the same in a `do { ... } while (0)` loop too, but they are different in other contexts. In PHP, `continue` and `break` are the same in a `switch` statement, but they are different in other contexts. –  Sep 10 '12 at 10:42
  • 1
    Well as @TheForestAndTheTrees pointed out from the manual, switch is considered a looping structure, and continue is made for looping structures. Unfortunately I've never see a switch 'loop', have you? – Aesthete Sep 10 '12 at 10:42
  • @hvd, wait... in c++ a do-while loop `continue` acts like a `break` command? now i am confused... i also dont understand the idea behind "loop structure" in the PHP's switch statement. if you use switch on PHP, does it simply loop through some sort of array, instead of epicly fast finds the correct path (like in c++, i assume) ?! – Rookie Sep 10 '12 at 10:46
  • 4
    @Rookie [Because that's how PHP rolls: Like a square wheel. – *Ignacio Vazquez-Abrams*](http://stackoverflow.com/questions/12151997/why-does-1234-1234-test-evaluate-to-true) :) – jrok Sep 10 '12 at 10:47
  • @jrok, well, i have a lot of experience on PHP, and i am familiar with the equality operator weirdnesses (etc), and im fine with those. But this just doesnt make any sense, really. PHP was meant to follow the C syntax, as far as i know. so why make the very basics work differently? do they just replace the switch(...) into for(...) while parsing the PHP code? lol. the continue just doesnt make any sense at all.. why would anyone use it in there. it would just make the program slower since it has to check every other cases below it, and none of those could be true anymore. – Rookie Sep 10 '12 at 10:52
  • @Rookie In a C++ `do { ... } while (0)` loop, not just any `do ... while` loop. Jumping to the end of the loop and jumping to just after the loop are the same thing, because the condition is always false. I just meant to point out that this is clearly not duplicating commands in C++, but for the same reason isn't duplicating commands in PHP. –  Sep 10 '12 at 10:53
  • @hvd, ah, i didnt notice the zero there :P well, of course it does that, since the loop ends there, stop confusing me ;) – Rookie Sep 10 '12 at 10:54
8

The documentation for the PHP continue statement makes this clear:

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

You should know that different languages give the same keywords subtly different meanings, and not assume that PHP continue behaves the same as C++ continue.

If continue makes sense in a PHP switch where it wouldn't work in C++, do use it.

If continue makes sense in a C++ switch where it wouldn't work in PHP, do use it.

  • " If a switch is inside a loop, continue 2 will continue with the next iteration of the outer loop." – John Jan 28 '20 at 22:19
2

As is warned in the PHP Manual:

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

So the use of continue will break out of the switch statement, not the for loop. The perils of assuming similar syntax across languages.

1

It is important to note that continue and break do not behave the same when the switch statement is nested within a loop. If you are using a switch statement to evaluate something, and want to move onto the next item within the loop if the condition is met, you should use continue 2. Using break 2 in this case will break out of the entire for loop, which may not be the desired action.

ZeLoubs
  • 215
  • 7
  • 19
0

Using continue inside a C++ switch / case construct that is embedded in a loop is perfectly OK. You shouldn't restrict your style in C++ just because of misbehavior occurring in other programming languages.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

In C and C++, the switch statement is only a fancy combination of if/else if and labels/goto, so using continue inside switch is okay. But as you noticed it doesn't do what you expect it to in other languages that are similar to C or C++. That's because they are only similar when it comes to syntax, for semantic rules they are very different beasts. So a thing that works in one language will most definitely not work in another even if the languages look similar.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621