Should I continue, or should i break?

This is one of the oldies out of my “PHP is weird”-box. I dug it up from an old post of mine in the GoT forums.

What would you expect this code to do?

for( $i = 1; $i <= 5; $i ++) {
    switch($i) {
        case 2: case 4:
    echo $i, "\n";

It should read



No it doesn’t. Read this:

continue continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration.

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


switch Note: Note that unlike some other languages, the continue statement applies to switch and acts similar to break. If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use continue 2.

😕 😕 😕

Read the original post at

Comments are disabled for this post