note 75936 deleted from control-structures.break by peehaa
| From: | peehaa@php.net | Date: | Wed, 22 Feb 2017 09:36:00 +0000 |
| Subject: | note 75936 deleted from control-structures.break by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208793@lists.php.net to get a copy of this message | ||
Note Submitter: pinkgothic at gmail dot com
----
To add to the responses given to "vlad at vlad dot neosurge dot net" - I'd like to
note the lack of automatic breaking in 'default' can be a very good thing. Consider this
useful snippet:
<?php
switch((string) $_REQUEST['mode']) {
default:
$_REQUEST['mode'] = "search";
// fall through...
case 'search' :
case 'list' :
case 'add' :
case 'edit' :
require(dirname(__FILE__)."/incs/".$_REQUEST['mode'].".php");
break;
}
?>
I personally find that far easier to look at than, for example:
<?php
$valid_modes = array('search','list','add','edit');
if (in_array($_REQUEST['mode'],$valid_modes)) {
require(dirname(__FILE__)."/incs/".$_REQUEST['mode'].".php");
} else {
require(dirname(__FILE__)."/incs/search.php");
}
?>
...and it even has the added benefit that the switch() variant only has one require() statement,
which makes for easier maintenance, e.g. if the directory changes, or what-have-you.
(Consider the above pseudocode, please, it's not tested - it's code illustrating a point
only.)