Bug #71905 [Nab]: invalid switch case is returning valid
| From: | pat at breakfree dot com | Date: | Sat, 26 Mar 2016 16:19:50 +0000 |
| Subject: | Bug #71905 [Nab]: invalid switch case is returning valid | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-200117@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71905&edit=1
ID: 71905
User updated by: pat at breakfree dot com
Reported by: pat at breakfree dot com
Summary: invalid switch case is returning valid
Status: Not a bug
Type: Bug
Package: *Programming Data Structures
Operating System: Ubuntu 14.04
PHP Version: 5.5.33
Block user comment: N
Private report: N
New Comment:
Ah - I was unaware of how fragile a weak comparison is.
I totally understand now. Thank you for the education.
Previous Comments:
------------------------------------------------------------------------
[2016-03-26 14:45:37] bwoebi@php.net
Switch cases are using weak comparisons and as 0 == "string" results in true, this is
expected behavior. Add an if (is_int($k)) continue; to solve your specific problem here.
------------------------------------------------------------------------
[2016-03-26 14:28:18] pat at breakfree dot com
Description:
------------
A false switch case is returning valid.
Perhaps related to key index returned from stat()?
PHP 5.5.9-1ubuntu4.14 (cli) (built: Oct 28 2015 01:34:46)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
Test script:
---------------
<?php
$s = stat('/tmp');
/*
* I was interested in data from a few keys. However I found the 0/dev key is always resulting as a
true case.
*/
$x = 0;
foreach($s as $k => $v) {
switch($k) {
case 'this_does_not_exist_and_should_never_occur':
echo 'Yet this case occurs: ' . $k . ', ' . $v . "\n";
$x++;
break;
default:
break;
}
}
echo "x=" . $x . "\n";
?>
Expected result:
----------------
x=0
Actual result:
--------------
Yet this case occurs: 0, 64769
x=1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71905&edit=1