note 124190 added to control-structures.switch
| From: | none at example dot com | Date: | Thu, 12 Sep 2019 05:31:07 +0000 |
| Subject: | note 124190 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212172@lists.php.net to get a copy of this message | ||
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).
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.54 (proxied: 209.6.125.175)
----
Manual Page -- http://php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/124190
Del: integrated -- https://master.php.net/note/delete/124190/integrated
Del: useless -- https://master.php.net/note/delete/124190/useless
Del: bad code -- https://master.php.net/note/delete/124190/bad+code
Del: spam -- https://master.php.net/note/delete/124190/spam
Del: non-english -- https://master.php.net/note/delete/124190/non-english
Del: in docs -- https://master.php.net/note/delete/124190/in+docs
Del: other reasons-- https://master.php.net/note/delete/124190
Reject -- https://master.php.net/note/reject/124190
Search -- https://master.php.net/manage/user-notes.php