PHP continue

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

A basic example that prints “13”, skipping over 2.

$arr = array( 1, 2, 3 );
foreach( $arr as $number ) {
    if( $number == 2 ) {
        continue;
    }
    echo $number;
}

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.

This code prints “outer, middle, inner. outer, middle, inner. outer, middle, inner. ”

$i = 0;
while ($i++ < 3) {
    echo "outer, ";
    while (1) {
        echo "middle, ";
        while (1) {
            echo "inner. ";
            continue 3;
        }
        echo "This never gets output.";
    }
    echo "Neither does this.";
}

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *