Vinsens
Vinsens

Reputation: 199

PHP - Difference between break and continue in switch case

What is the different between

switch (variable) {
        case 'value':
        # code...
        break;

        case 'value':
        # code...
        break;
}

and this one

switch (variable) {
        case 'value':
        # code...
        continue;

        case 'value':
        # code...
        continue;
}

It's really different result or just same?

Upvotes: 3

Views: 4577

Answers (3)

Akshay Khale
Akshay Khale

Reputation: 8361

Here is a simple example code with both the switch cases mentioned above

<?php
    $variable = 20;
    echo "Normal<br/>";
    switch ($variable) {
        case '20':
            echo "twenty";
        break;
        case '21':
            echo "twenty one";
        break;
    }

    echo "<br/><br/>With Continue<br/>";
    switch ($variable) {
        case '20':
            echo "twenty";
        continue;
        case '21':
            echo "twenty one";
        continue;
    }
?>

When I execute above code I got following output

Normal
twenty

With Continue
twenty

How?

Working of break statement

Break statement causes code execution to come out of the block and execute next statements because of which switch statement will execute only one case statement and exit out of switch block without executing other case blocks.

Working of Continue statement

Continue statement in case of loops will cause loop to stop execution of current iteration of loop and go for next iteration of the loop(if any exists) but in case of switch statement it is considered as a loop statement but no next iterations causing exit switch statement.

We can have a switch statement without break statements too like this

<?php
    $variable = 20;
    echo "Normal";
    switch ($variable) {
        case '19':
            echo "<br/>Nineteen";
        case '20':
            echo "<br/>twenty";
        case '21':
            echo "<br/>twenty one";
        case '23':
            echo "<br/>twenty three";
    }
?>

The output of above code will be

Normal
twenty
twenty one
twenty three

i.e. executing all the case statements after the case where first match is found.

Upvotes: 0

Technoh
Technoh

Reputation: 1600

This is a special case for PHP because, as stated by the official documentation:

Note: In PHP the switch statement is considered a looping structure for the purposes of continue. continue behaves like break (when no arguments are passed). If a switch is inside a loop, continue 2 will continue with the next iteration of the outer loop.

So in essence it means there is no actual difference between your two examples. However for clarity I think it would be best to use break as that is the standard in other languages. Note also that you could use continue 2 (or 3, 4...) to advance to the next iteration of a loop if the switch is inside a loop (or more).

Upvotes: 2

Harikrishnan
Harikrishnan

Reputation: 9979

In PHP the above two codes work in same way. Here the break and continue statement prevent control going to next case. That is the continue acts just like break here. Also switch is intended to be executed only once. It's not a loop. Hence continue is not relevant here.

Note:If there is loop enclosing this switch statement then the result will be different.

Upvotes: 1

Related Questions