Bug #69558 [Nab]: switch statement output unexpected
| From: | requinix@php.net | Date: | Fri, 01 May 2015 18:10:38 +0000 |
| Subject: | Bug #69558 [Nab]: switch statement output unexpected | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192441@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69558&edit=1
ID: 69558
Updated by: requinix@php.net
Reported by: stephane at winnepenninckx dot com
Summary: switch statement output unexpected
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: mac os x 10.10
PHP Version: 5.6.8
Block user comment: N
Private report: N
New Comment:
If they think it's a bug then they must not have as much experience with PHP as they claim.
"switch (X) case Y:" is equivalent to "if (X == Y)", so what you've written
is like
if ($i == ($i < 1)) {
} else if ($i == ($i < 3)) {
} etc
Obviously that's not what you wanted.
A switch doesn't make sense for that. It's most useful for dealing with specific values,
like if you wanted different behavior for specific values of $i, and not for general conditional
logic. Use if instead.
Previous Comments:
------------------------------------------------------------------------
[2015-05-01 17:57:59] stephane at winnepenninckx dot com
I've asked around and people with lot of experience in PHP asked me to file a bug, because they
believe it is one.
It 's too easy to say it is not bug.
------------------------------------------------------------------------
[2015-05-01 16:24:08] nikic@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
------------------------------------------------------------------------
[2015-05-01 15:37:58] stephane at winnepenninckx dot com
Description:
------------
the case $i < 1 down not occur for $i = 0, the script goes to the default case:
-----------------
0 : Oops
-----------------
If you delete the default case :
-----------------
0 : quite bad!
-----------------
I try with the for loop modified :
for ($i = 10.1; $i >= 0; --$i)
then the case $i<1 occurs :
-----------------
0.1 : really!!
-----------------
It looks like there is a problem with the value 0 when comparing to 1
also in older versions of PHP like 5.5
Test script:
---------------
$sep = PHP_EOL.'-----------------'.PHP_EOL;
echo 'switch demo';
echo $sep;
for ($i = 10; $i >= 0; --$i) {
switch($i) {
case $i < 1 :
$msg = 'really!!';
break;
case $i < 3 :
$msg = 'quite bad!';
break;
case $i < 5 :
$msg = 'pay attention!';
break;
case $i <= 9 :
$msg = 'quite good';
break;
case $i <= 10 :
$msg = 'congrat';
break;
default:
$msg = 'Oops';
break;
}
echo $i . ' : '.$msg.$sep;
}
Expected result:
----------------
when $i = 0 the $msg should be 'really!!' not 'Oops':
-----------------
0 : really!!
-----------------
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69558&edit=1