note 90836 added to control-structures.switch
| From: | Booya at osu1 dot php dot net | Date: | Tue, 12 May 2009 12:28:43 +0000 |
| Subject: | note 90836 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154498@lists.php.net to get a copy of this message | ||
@sedativchunk: It's important to note that your use of switch is misleading. It may leave
people under the impression that "case" acts like "if", which is very much not
the case.
Case compares the value passed to switch with the _result_ of the expression you're giving it.
Your case statements are in fact a set of booleans, of which only one is TRUE at any given time (in
this particular setup). So the switch ends up doing loose comparison between a random integer in the
range 1-50 with these boolean values and, naturally, matches against the first one that's TRUE
(because loose comparison considers that non-zero integers are "equal" to boolean TRUE).
Like I said, this use of switch is quite misleading and only serves to obfuscate the code.
You're much better off using if/elseif/else in this case.
----
Server IP: 69.147.83.197
Probable Submitter: 194.88.148.6
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/90836
Del: integrated -- https://master.php.net/note/delete/90836/integrated
Del: useless -- https://master.php.net/note/delete/90836/useless
Del: bad code -- https://master.php.net/note/delete/90836/bad+code
Del: spam -- https://master.php.net/note/delete/90836/spam
Del: non-english -- https://master.php.net/note/delete/90836/non-english
Del: in docs -- https://master.php.net/note/delete/90836/in+docs
Del: other reasons-- https://master.php.net/note/delete/90836
Reject -- https://master.php.net/note/reject/90836
Search -- https://master.php.net/manage/user-notes.php