note 104212 added to control-structures.switch
| From: | bm13kk at gmail dot com | Date: | Wed, 01 Jun 2011 09:52:00 +0000 |
| Subject: | note 104212 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180157@lists.php.net to get a copy of this message | ||
I've read 2 comments (http://ua2.php.net/manual/en/control-structures.switch.php#93342, http://ua2.php.net/manual/en/control-structures.switch.php#88643)
with the code like:
<?php
switch ($foo) {
case ($foo<1): echo 'a'; break;
case ($foo>1): echo 'b'; break;
case ($foo>10): echo 'c'; break;
}
?>
But it is wrong. It prints 'b' because code above is equivalent
<?php
if ($foo == ($foo<1)) {echo 'a';}
if ($foo == ($foo>1)) {echo 'b';}
if ($foo == ($foo>10)) {echo 'c';}
?>
If you realy dislike elseif, use this style
<?php
switch (TRUE) {
case ($foo<1): break;
case ($foo>1): break;
case ($foo>10): break;
}
?>
----
Server IP: 89.251.21.32
Probable Submitter: 217.12.212.111
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/104212
Del: integrated -- https://master.php.net/note/delete/104212/integrated
Del: useless -- https://master.php.net/note/delete/104212/useless
Del: bad code -- https://master.php.net/note/delete/104212/bad+code
Del: spam -- https://master.php.net/note/delete/104212/spam
Del: non-english -- https://master.php.net/note/delete/104212/non-english
Del: in docs -- https://master.php.net/note/delete/104212/in+docs
Del: other reasons-- https://master.php.net/note/delete/104212
Reject -- https://master.php.net/note/reject/104212
Search -- https://master.php.net/manage/user-notes.php