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:
            continue;
    }
    echo $i, "\n";
}

It should read

1
3
5

right?

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 drm.tweakblogs.net

This entry was posted in Development and tagged . Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.