Bug #72746 [NEW]: Switch default case

From: Date: Wed, 03 Aug 2016 15:24:13 +0000
Subject: Bug #72746 [NEW]: Switch default case
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202882@lists.php.net to get a copy of this message
From: dkruger at pinnaclepub dot com Operating system: CentOS 7 PHP version: 5.6.24 Package: Unknown/Other Function Bug Type: Bug Bug description:Switch default case Description: ------------ Hello, this is a pretty basic issue, basically, it seems that when using an array with a mix of strings and integers or default keys (0, 1, 2 etc), if using foreach to iterate through each element of the array intending for something specific to happen when the string key is hit, and having a 0 element in the array, the 0 element is running what should only be happening when that string element is in the array. I do not believe this issue had occurred in previous versions of php but I had just recently updated to 5.6.24 from 5.6.21. Thank you, Test script: --------------- <?php // Test for switch not running default code when array element key is 0 if there is a mix of array keys that are numerical along with a string array key $test = array(0 => "First element, should be 0",1 => "Second element, should be 1",2 => "Third element, should be 2","Three" => "Fourth element, should be Three"); print_r($test); foreach($test as $k => $v) { switch($k) { case "Three": echo "not to be printed\n"; break; default: echo $k.": ".$v."\n"; break; } } ?> Expected result: ---------------- Array ( [0] => First element, should be 0 [1] => Second element, should be 1 [2] => Third element, should be 2 [Three] => Fourth element, should be Three ) 0: First element, should be 0 1: Second element, should be 1 2: Third element, should be 2 not to be printed Actual result: -------------- Array ( [0] => First element, should be 0 [1] => Second element, should be 1 [2] => Third element, should be 2 [Three] => Fourth element, should be Three ) not to be printed 1: Second element, should be 1 2: Third element, should be 2 not to be printed -- Edit bug report at https://bugs.php.net/bug.php?id=72746&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72746&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72746&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72746&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=72746&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=72746&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=72746&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=72746&r=needscript Try newer version: https://bugs.php.net/fix.php?id=72746&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=72746&r=support Expected behavior: https://bugs.php.net/fix.php?id=72746&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=72746&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=72746&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=72746&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72746&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=72746&r=dst IIS Stability: https://bugs.php.net/fix.php?id=72746&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=72746&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=72746&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=72746&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=72746&r=mysqlcfg

« previous php.bugs (#202882) next »