note 41767 deleted from control-structures.switch by crell

From: Date: Mon, 27 Jun 2022 21:55:34 +0000
Subject: note 41767 deleted from control-structures.switch by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218654@lists.php.net to get a copy of this message
Note Submitter: manicdepressive at mindless dot com ---- Be careful if distinguishing between NULL and (int)0. As implied in the above documentation, the case statements are equivalent to the '==' operator, not the '===' operator, so the following code did not work as i expected: <?php $mixed = 0; switch($mixed){ case NULL: echo "NULL"; break; case 0: echo "zero"; break; default: echo "other"; break; } ?> Instead, I may use a chain of else-ifs. (On this page, kriek at jonkreik dot com states that "in most cases [a switch statement] is 15% faster [than an else-if chain]" but jemore at m6net dotdot fr claims that when using ===, if/elseif/elseif can be 2 times faster than a switch().) Alternatively, if i prefer the appearance of the switch() statement I may use a trick like the one nospam at please dot com presents: <?php $mixed = 0; switch(TRUE){ case (NULL===$mixed): //blah break; case (0 ===$mixed): //etc. break; } ?> code till dawn! mark meves!

« previous php.notes (#218654) next »