note 120342 deleted from control-structures.switch by cmb
| From: | cmb@php.net | Date: | Fri, 16 Dec 2016 23:03:54 +0000 |
| Subject: | note 120342 deleted from control-structures.switch by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208611@lists.php.net to get a copy of this message | ||
Note Submitter: my at example dot com
----
One thing that isn't explicitly stated about switch is that, whether blank or not, if you
don't include break, it will always proceed to the next case.
<?php
foreach (array(0, 1, 2, 3, 4) as $foo) {
switch($foo) {
case 1: echo 'a';
case 0: echo '0'; break;
case 2: echo 'b';
case 3: echo 'c';
case 4: echo 'd';
default: echo '<BR>';
}
}
Will return:
abcd
0bcd
cd
d
This can be useful in certain cases, when certain values should be supersets of other values, and
can be cleaner than the corresponding if statement. You can use this to call functions and
effectively have multiple default cases.
<?php
switch($install_step) {
case MY_CONST_DOWNLOAD: download();
case MY_CONST_INSTALL: install();
case MY_CONST_CLEANUP: cleanup();
case MY_CONST_EMAIL_USER: emailUser();
}