php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30285 switch statement: default executed when case matches
Submitted: 2004-09-30 05:25 UTC Modified: 2004-10-13 03:50 UTC
Votes:78
Avg. Score:4.4 ± 1.4
Reproduced:29 of 43 (67.4%)
Same Version:20 (69.0%)
Same OS:14 (48.3%)
From: benjcarson at digitaljunkies dot ca Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2004-09-30 (dev) OS: Debian Linux
Private report: No CVE-ID: None
 [2004-09-30 05:25 UTC] benjcarson at digitaljunkies dot ca
Description:
------------
It seems that the default: block of a switch statement now short-circuits the remaining case: tests.  If the default: block is put before a matching case: block, it will _always_ be executed.  While the docs (http://www.php.net/manual/en/control-structures.switch.php) do recommend making default: last, this is a BC break (tested with 5.0.0 and 4.3.2).

This bug is also present at least as early as the 5.1.x-dev 2004-09-25 snap (sorry, the next earliest one I have is 5.0.0).

The reproduce code below works if the default: block is put after the case "a": block.

Reproduce code:
---------------
<?php
$x = "a";
switch ($x) {
 default:
    echo "default\n";
    break;
  case "a":
    echo "case: a\n";
    break;
}
?>

Expected result:
----------------
case: a

Actual result:
--------------
default

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-08 16:25 UTC] Jared dot Williams1 at ntlworld dot com
5.0.2 does produce the expected result, as does 4.3.8
 [2004-10-08 22:49 UTC] paco at arsteca dot net
got expected result with 4.3.2, including CLI
 [2004-10-13 03:34 UTC] dev at null dot dk
got expected result in php version 4.3.1 running under apache.
 [2004-10-13 03:50 UTC] benjcarson at digitaljunkies dot ca
As per discussion on internals@lists.php.net, this has been fixed in CVS (5.1.x-dev).  Thanks Andi.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Feb 18 00:01:25 2020 UTC