Bug #71905 [Opn->Nab]: invalid switch case is returning valid

From: Date: Sat, 26 Mar 2016 14:45:41 +0000
Subject: Bug #71905 [Opn->Nab]: invalid switch case is returning valid
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200115@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 Updated by: bwoebi@php.net Reported by: pat at breakfree dot com Summary: invalid switch case is returning valid -Status: Open +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: 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. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#200115) next »