note 29465 deleted from control-structures.switch by peehaa

From: Date: Mon, 02 Dec 2013 09:17:51 +0000
Subject: note 29465 deleted from control-structures.switch by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197704@lists.php.net to get a copy of this message
Note Submitter: jemore at m6net dotdot fr ---- siwtch() made always a type conversion before comparing all the case value (PHP4.3.0), so the following statement <?php // $a = 'abc0' or 'abc1' or 'abc2', so this is a string switch ($a) { case 'abc0' : $nb += 1; break; case 'abc1' : $nb += 2; break; case 'abc2' : $nb += 3; break; } ?> is slower than the following statement <? if ($a === 'abc0') $nb += 1; elseif ($a === 'abc1') $nb += 2; elseif ($a === 'abc2') $nb += 3; ?> because the '===' (3 equals signs) compare value without type conversion. Using a if/elseif/elseif structure instead of switch/case/case can be 2 times faster (I have made a test)

« previous php.notes (#197704) next »