note 88770 added to control-structures.switch
| From: | admin at edogs dot ru | Date: | Fri, 06 Feb 2009 21:00:12 +0000 |
| Subject: | note 88770 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150319@lists.php.net to get a copy of this message | ||
I must confess, that "sedativchunk at gmail dot com" in his comment at 02-Feb-2009 a bit
wrong.
Let's take simple example
$a=0;
switch($a) {
case ($a < 100):echo 1;break;
case ($a > 100):echo 2;break;
}
this will output 2 (TWO!). Seems strange? Nope at all.
Switch is NOT simular to
if($a<100) echo 1; elseif($a>100) echo 2;
It's simular to
if($a==($a<100)) echo 1; else ($a==($a>100)) echo 2;
becase it's COMPRASSION to VALUE in switch.
switch($bac) case $b*10 will compare $bac to $b*10
if you wanna use switch such way, use
switch(true) {
case ($a < 100):echo 1;break;
case ($a > 100):echo 2;break;
}
because (true==($a<100)) is simular to $a<100
/edogs
----
Server IP: 78.41.104.32
Probable Submitter: 89.112.26.63
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/88770
Del: integrated -- https://master.php.net/note/delete/88770/integrated
Del: useless -- https://master.php.net/note/delete/88770/useless
Del: bad code -- https://master.php.net/note/delete/88770/bad+code
Del: spam -- https://master.php.net/note/delete/88770/spam
Del: non-english -- https://master.php.net/note/delete/88770/non-english
Del: in docs -- https://master.php.net/note/delete/88770/in+docs
Del: other reasons-- https://master.php.net/note/delete/88770
Reject -- https://master.php.net/note/reject/88770
Search -- https://master.php.net/manage/user-notes.php