note 91213 deleted from control-structures.switch by danbrown
| From: | danbrown@php.net | Date: | Sat, 30 May 2009 13:57:01 +0000 |
| Subject: | note 91213 deleted from control-structures.switch by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156181@lists.php.net to get a copy of this message | ||
Note Submitter: wbeaumo1 at gmail dot com
----
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;
}
?>