Edit report at https://bugs.php.net/bug.php?id=68346&edit=1

 ID:                 68346
 Updated by:         requi...@php.net
 Reported by:        codingcarlos at gmail dot com
 Summary:            Bug on switch in case 0
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Testing related
 Operating System:   windows/linux
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

"switch(X) case Y" is like saying "if (X == Y)". So what you've done is create
  if ($number == $number < 2) { ...
  } else if ($number == $number < 5) { ...
  } else if ($number == $number >= 5) { ...
  }
which is obviously not what you intended.

You can't do a "case < 2" but you can do
  switch (true) {
    case ($number<2):  // if (true == $number < 2)
    case ($number<5):  // if (true == $number < 5)
    case ($number>=5): // if (true == $number >= 5)
  }


Previous Comments:
------------------------------------------------------------------------
[2014-11-05 09:36:15] codingcarlos at gmail dot com

Description:
------------
When a variable is stored via $_GET method (even when parsing to integer) and 
used in a switch, fails on case 0.

When you use it like: example.php?num=0 
You will get the result: "The number 0 is higer than 5." (Obiously fail)

But if you use 00 instead of 0: example.php?num=00 
You will get the result: "The number 00 is lower than 2." (Which is true)

It also works with " 0": example.php?num= 0
You will get the result: "The number 00 is lower than 2." (Which is true)

Test script:
---------------
$number = $_GET['num'];
// $number = intval($_GET['num']);
// $number = (int) $_GET['num'];

    switch ($number) {
        case ($number<2): 
            $result = 'lower than 2'; 
            break;
        case ($number<5): 
            $result = 'lower than 5'; 
            break;
        case ($number>=5): 
            $result = 'higer than 5'; 
            break; // Se mete aqui con nota = 10
    }
    echo "The number " . $number . " is " . $result . ".";

Expected result:
----------------
The number 0 is lower than 2.

Actual result:
--------------
The number 0 is higer than 5.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=68346&edit=1

-- 
PHP Quality Assurance Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to