statement - php switch case integer



Should I ever use continue inside a switch statement? (5)

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
*/

https://src-bin.com


Answer #1

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.


Answer #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.


Answer #3

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.


Answer #4

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!


Answer #5

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.





switch-statement