note 127951 added to control-structures.switch
| From: | j dot kane dot third at gmail dot com | Date: | Tue, 22 Nov 2022 19:46:09 +0000 |
| Subject: | note 127951 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219906@lists.php.net to get a copy of this message | ||
The default case appears to always be evaluated last. If break is excluded from the default case,
then the proceeding cases will be reevaluated. This behavior appears to be undocumented.
<?php
$kinds = ['moo', 'kind1', 'kind2'];
foreach ($kinds as $kind) {
switch($kind)
{
default:
// The kind wasn't valid, set it to the default
$kind = 'kind1';
var_dump('default');
case 'kind1':
var_dump('1');
break;
case 'kind2':
var_dump('2');
break;
case 'kindn':
var_dump('n-th');
break;
}
echo "\n\n";
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 54.84.255.89)
----
Manual Page -- https://php.net/manual/en/control-structures.switch.php
Edit -- https://main.php.net/note/edit/127951
Del: integrated -- https://main.php.net/note/delete/127951/integrated
Del: useless -- https://main.php.net/note/delete/127951/useless
Del: bad code -- https://main.php.net/note/delete/127951/bad+code
Del: spam -- https://main.php.net/note/delete/127951/spam
Del: non-english -- https://main.php.net/note/delete/127951/non-english
Del: in docs -- https://main.php.net/note/delete/127951/in+docs
Del: other reasons-- https://main.php.net/note/delete/127951
Reject -- https://main.php.net/note/reject/127951
Search -- https://main.php.net/manage/user-notes.php