note 41767 added to control-structures.switch

From: Date: Wed, 21 Apr 2004 20:43:11 +0000
Subject: note 41767 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-68481@lists.php.net to get a copy of this message
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! ---- Manual Page -- http://www.php.net/manual/en/control-structures.switch.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41767 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41767&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41767&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#68481) next »