note 105405 added to control-structures.switch

From: Date: Mon, 15 Aug 2011 21:28:55 +0000
Subject: note 105405 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-182498@lists.php.net to get a copy of this message
In response to varnavruz, his example script will never show "That's slooooow" as a reply since the switch structure will pick the first case is satisfied. Therefore, if $totaltime is greater than 10, it will also be greater than 1, thereby always showing "Not fast!" as a reply. Plus, the value 1 is not accounted for, so a greater than or equal to must be used somwhere if a test for 1 itself is not present. A simple re-ordering of the case scenarios will fix this issue: <?php switch ($totaltime) { case ($totaltime < 1): echo "That was fast!"; break; case ($totaltime > 10): echo "That's slooooow"; break; case ($totaltime >= 1): echo "Not fast!"; break; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 66.29.169.138 ---- Manual Page -- http://www.php.net/manual/en/control-structures.switch.php Edit -- https://master.php.net/note/edit/105405 Del: integrated -- https://master.php.net/note/delete/105405/integrated Del: useless -- https://master.php.net/note/delete/105405/useless Del: bad code -- https://master.php.net/note/delete/105405/bad+code Del: spam -- https://master.php.net/note/delete/105405/spam Del: non-english -- https://master.php.net/note/delete/105405/non-english Del: in docs -- https://master.php.net/note/delete/105405/in+docs Del: other reasons-- https://master.php.net/note/delete/105405 Reject -- https://master.php.net/note/reject/105405 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#182498) next »