note 29465 added to control-structures.switch
| From: | jemore at rack1 dot php dot net | Date: | Fri, 14 Feb 2003 10:26:02 +0000 |
| Subject: | note 29465 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43926@lists.php.net to get a copy of this message | ||
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)
--
http://www.php.net/manual/en/control-structures.switch.php
http://master.php.net/manage/user-notes.php?action=edit+29465
http://master.php.net/manage/user-notes.php?action=delete+29465
http://master.php.net/manage/user-notes.php?action=reject+29465