note 90836 deleted from control-structures.switch by danbrown

From: Date: Tue, 12 May 2009 12:36:16 +0000
Subject: note 90836 deleted from control-structures.switch by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154499@lists.php.net to get a copy of this message
Note Submitter: Booya ---- @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.

« previous php.notes (#154499) next »