melp.nl

< Return to main page

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?

#!php
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


< Return to main page


You're looking at a very minimalistic archived version of this website. I wish to preserve to content, but I no longer wish to maintain Wordpress, nor handle the abuse that comes with that.