note 114274 added to control-structures.switch
| From: | Gutza | Date: | Fri, 31 Jan 2014 22:05:52 +0000 |
| Subject: | note 114274 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198378@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 188.241.113.74
Probable Submitter: 85.204.57.67
----
Manual Page -- http://php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/114274
Del: integrated -- https://master.php.net/note/delete/114274/integrated
Del: useless -- https://master.php.net/note/delete/114274/useless
Del: bad code -- https://master.php.net/note/delete/114274/bad+code
Del: spam -- https://master.php.net/note/delete/114274/spam
Del: non-english -- https://master.php.net/note/delete/114274/non-english
Del: in docs -- https://master.php.net/note/delete/114274/in+docs
Del: other reasons-- https://master.php.net/note/delete/114274
Reject -- https://master.php.net/note/reject/114274
Search -- https://master.php.net/manage/user-notes.php