Bug #72746 [Opn->Nab]: Switch default case
| From: | requinix@php.net | Date: | Wed, 03 Aug 2016 15:53:10 +0000 |
| Subject: | Bug #72746 [Opn->Nab]: Switch default case | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202885@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72746&edit=1
ID: 72746
Updated by: requinix@php.net
Reported by: dkruger at pinnaclepub dot com
Summary: Switch default case
-Status: Open
+Status: Not a bug
Type: Bug
-Package: Unknown/Other Function
+Package: Scripting Engine problem
Operating System: CentOS 7
PHP Version: 5.6.24
Block user comment: N
Private report: N
New Comment:
This behavior has been present in all versions of PHP as far back as I can remember. https://3v4l.org/mU9Fr
switch does a loose comparison. 0 == "Three" so that case will be executed.
http://php.net/manual/en/types.comparisons.php#types.comparisions-loose
(your situation is "php" <-> 0)
If you only want the switch to be evaluated for string keys when you know that there are integer
keys as well (something I don't recommend), consider wrapping the switch in an
if (is_string($k)) {
Alternatively you can do
switch(true) {
case $k === "Three":
but at that point you might as well just do a series of ifs.
Previous Comments:
------------------------------------------------------------------------
[2016-08-03 15:24:08] dkruger at pinnaclepub dot com
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 this bug report at https://bugs.php.net/bug.php?id=72746&edit=1