note 91213 added to control-structures.switch
| From: | wbeaumo1atgmaildotcom at osu1 dot php dot net | Date: | Sat, 30 May 2009 09:40:34 +0000 |
| Subject: | note 91213 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156178@lists.php.net to get a copy of this message | ||
RE: sedativchunk
Your switch is taking an integer as input and evaluating boolean cases! It only works by accident.
Since you've defined your input ($randomizer) as always being a positive nonzero integer, and
since a nonzero integer always evaluates to "true" when compared with a boolean, it
follows that only cases that evaluate to "true" will be executed. But what if you changed
the rules so that your $randomizer could be zero? Then if it were zero, only cases that evaluated to
"false" would be executed! To avoid that possibility, just change the switch input to
"true":
<?php
$randomizer = rand(1,50);
switch(true)
{
case ($randomizer <= 20):
$font_size = "11";
break;
case ($randomizer <= 30):
$font_size = "16";
break;
case ($randomizer <= 40):
$font_size = "18";
break;
case ($randomizer <= 50):
$font_size = "20";
break;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 75.82.195.12
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/91213
Del: integrated -- https://master.php.net/note/delete/91213/integrated
Del: useless -- https://master.php.net/note/delete/91213/useless
Del: bad code -- https://master.php.net/note/delete/91213/bad+code
Del: spam -- https://master.php.net/note/delete/91213/spam
Del: non-english -- https://master.php.net/note/delete/91213/non-english
Del: in docs -- https://master.php.net/note/delete/91213/in+docs
Del: other reasons-- https://master.php.net/note/delete/91213
Reject -- https://master.php.net/note/reject/91213
Search -- https://master.php.net/manage/user-notes.php