php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69558 switch statement output unexpected
Submitted: 2015-05-01 15:37 UTC Modified: 2015-05-01 19:10 UTC
From: stephane at winnepenninckx dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.8 OS: mac os x 10.10
Private report: No CVE-ID: None
 [2015-05-01 15:37 UTC] 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!!
-----------------


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-01 16:24 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-05-01 16:24 UTC] 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 17:57 UTC] 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 18:10 UTC] requinix@php.net
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.
 [2015-05-01 19:00 UTC] stephane at winnepenninckx dot com
I would like that you are right. But I rewrote the switch with 'else if' the way you pretend the switch statement is working and I ended with a different result.


for ($i = 10; $i >= 0; --$i){
	if ($i == ($i < 1)){
		$msg = 'really';
	}else if ($i == ($i < 3)) {
		$msg = 'quite bad';
	}else if ($i == ($i < 5)) {
		$msg = 'pay attention';
	}else if ($i == ($i <= 9)){
		$msg = 'quite good';
	}else if ($i == ($i <= 10)){
		$msg = 'congrat';
	}else{
		$msg == 'Oops';
	}

	echo $i.' '.$msg . $sep;

}

for $i with value '04 it returns: '0 quite bad' where the switch statement returns '0 : Oops'
IMHO this is different.
 [2015-05-01 19:10 UTC] nikic@php.net
You have a typo: $msg == 'Oops' instead of $msg = 'Oops'. You'll get the expected result if you fix this.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 17:00:02 2026 UTC