Home » Php » php – 'break' from a switch, then 'continue' in a loop

php – 'break' from a switch, then 'continue' in a loop

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is it possible to break from a switch and then continue in a loop?

For example:

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');

foreach($letters as $letter) {
    foreach($numbers as $number) {
        switch($letter) {
           case 'd':
               // So here I want to 'break;' out of the switch, 'break;' out of the
               // $numbers loop, and then 'continue;' in the $letters loop.
               break;
        }
    }

    // Stuff that should be done if the 'letter' is not 'd'.

}

Can this be done, and what would the syntax be?

How to&Answers:

You want to use break n

break 2;

After clarification, looks like you want continue 2;

Answer:

Instead of break, use continue 2.

Answer:

I know this is a serious necro but… as I arrived here from Google figured I’d save others the confusion.

If he meant breaking out from the switch and just ending the number’s loop, then break 2; would’ve been fine. continue 2; would just continue the number’s loop and keep iterating through it just to be continue‘d each time.

Ergo, the correct answer ought to be continue 3;.

Going by a comment in the docs continue basically goes to the end of the structure, for switch that’s that (will feel the same as break), for loops it’d pick up on the next iteration.

See: http://codepad.viper-7.com/dGPpeZ

Example in case above n/a:

<?php
    echo "Hello, World!<pre>";

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');

$i = 0;
foreach($letters as $letter) {
    ++$i;
    echo $letter . PHP_EOL;
    foreach($numbers as $number) {
        ++$i;
        switch($letter) {
           case 'd':
               // So here I want to 'break;' out of the switch, 'break;' out of the
               // $numbers loop, and then 'continue;' in the $letters loop.
              continue 3; // go to the end of this switch, numbers loop iteration, letters loop iteration
            break;
           case 'f':
            continue 2; // skip to the end of the switch control AND the current iteration of the number's loop, but still process the letter's loop
            break;
           case 'h':
            // would be more appropriate to break the number's loop
            break 2;

        }
        // Still in the number's loop
        echo " $number ";
    }


    // Stuff that should be done if the 'letter' is not 'd'.
    echo " $i " . PHP_EOL;

}

Results:

Hello, World!
a
 1  2  3  4  5  6  7  8  9  0  11 
b
 1  2  3  4  5  6  7  8  9  0  22 
c
 1  2  3  4  5  6  7  8  9  0  33 
d
e
 1  2  3  4  5  6  7  8  9  0  46 
f
 57 
g
 1  2  3  4  5  6  7  8  9  0  68 
h
 70 
i
 1  2  3  4  5  6  7  8  9  0  81 

continue 2; not only processes the letter’s loop for the letter d but even processes the rest of the number’s loop (note that $i is both incremented and printed after f). (Which may or may not be desirable…)

Hope that helps anyone else that winds up here first.