note 124190 deleted from control-structures.switch by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 19:42:14 +0000 |
| Subject: | note 124190 deleted from control-structures.switch by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218739@lists.php.net to get a copy of this message | ||
Note Submitter: none at example dot com
----
A note on continue, continue 2, and break:
$a = [1, 2, 3];
foreach($a as $b) {
switch($b) {
case 1:
echo 'a'; continue;
case 2:
echo 'b'; continue;
default:
echo 'c'; continue;
}
echo '0';
}
// outputs a0b0c0 (and warns twice about continue in a switch statement)
foreach($a as $b) {
switch($b) {
case 1:
echo 'a'; break;
case 2:
echo 'b'; break;
default:
echo 'c'; break;
}
echo '0';
}
// outputs a0b0c0
foreach($a as $b) {
switch($b) {
case 1:
echo 'a'; continue 2;
case 2:
echo 'b'; continue 2;
default:
echo 'c'; continue 2;
}
echo '0';
}
//outputs abc0
In short, continue and break will more or less behave the same (albeit the former throws out
warnings), but continue 2 will end the loop iteration, and move to the next iteration (even if there
is a block in the iteration after the switch statement).