note 114274 deleted from control-structures.switch by cmb
| From: | cmb@php.net | Date: | Sun, 21 Apr 2019 11:53:37 +0000 |
| Subject: | note 114274 deleted from control-structures.switch by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210648@lists.php.net to get a copy of this message | ||
Note Submitter: Gutza
----
Sometimes you need to execute a common piece of code for several cases, and then execute some
special code for each of those cases in particular. You'd then be tempted to duplicate
"case" entries, like so:
<?php
$health = 50;
$prevActionType = "none";
$action = "kill";
switch($action) {
case "heal":
$prevActionType = "good";
$health += 10;
break;
case "hurt":
case "kill":
$prevActionType = "bad";
break;
case "hurt":
$health -= 10;
break;
case "kill":
$health = 0;
break;
}
?>
That won't work as intended -- you'll enter at the first case that matches
($prevActionType = "bad") and then exit the switch altogether.