note 63366 deleted from control-structures.switch by betz
| From: | betz@php.net | Date: | Wed, 29 Mar 2006 02:19:13 +0000 |
| Subject: | note 63366 deleted from control-structures.switch by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106894@lists.php.net to get a copy of this message | ||
Note Submitter: fg at NOSPAM dot puertodelacruz dot org
----
Use of break after return.
It may be obvious, but many user samples insist exposing unreachable code.
If you use return, subsequent code will not be executed. So break statements are not needed.
$a=1;
echo foo($a);
function foo($a){
switch($a){
case 1:
return 'Desperate value returned!';
$retval='One';
break;
case 2:
$retval='Two';
break;
default:
$retval='Any other value';
}
return $retval;
}
foo return 'Desperate...'.
The code execution does not reach the lines following the return, so break; is superfluous.
The code:
function foo($a){
switch($a){
case 1:
return 'One';
case 2:
$retval='Two';
break;
default:
$retval='Any other value';
}
return $retval;
}
will do the work as well.
Final and helping code should be a bit light and cleaner...