Bug #72087 [Opn->Nab]: case $var !== 0 return wrong answer (only with 0)
| From: | requinix@php.net | Date: | Sat, 23 Apr 2016 16:58:33 +0000 |
| Subject: | Bug #72087 [Opn->Nab]: case $var !== 0 return wrong answer (only with 0) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-200719@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72087&edit=1
ID: 72087
Updated by: requinix@php.net
Reported by: sklogika2 at gmail dot com
Summary: case $var !== 0 return wrong answer (only with 0)
-Status: Open
+Status: Not a bug
Type: Bug
Package: *Programming Data Structures
Operating System: Centos7
PHP Version: 5.6.20
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 the same as writing "if ($x == $y)". What you've
written there is "if ($userId == ($userId !== 0))".
You should use
switch ($userId) {
case 0:
echo "zero";
break;;
default:
echo "not zero";
break;
}
Previous Comments:
------------------------------------------------------------------------
[2016-04-23 16:46:26] sklogika2 at gmail dot com
I'm sorry instead of case 'userIdKOZqAfk': should be default:
But idea is about !=0
------------------------------------------------------------------------
[2016-04-23 16:44:00] sklogika2 at gmail dot com
Description:
------------
In the code below I get result of "not zero" when I should get "zero"
Such case comparisons do not work properly only with 0, with other numbers it is ok
Test script:
---------------
$userId = 0;
switch ($userId) {
case $userId !== 0:
echo "not zero";
break;
case 'userIdKOZqAfk':
echo "zero";
break;
}
Expected result:
----------------
zero
Actual result:
--------------
not zero
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72087&edit=1