php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72746 Switch default case
Submitted: 2016-08-03 15:24 UTC Modified: 2016-08-03 15:53 UTC
From: dkruger at pinnaclepub dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.24 OS: CentOS 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dkruger at pinnaclepub dot com
New email:
PHP Version: OS:

 

 [2016-08-03 15:24 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-03 15:53 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: Unknown/Other Function +Package: Scripting Engine problem
 [2016-08-03 15:53 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 13:00:02 2026 UTC