php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67757 multiple default cases
Submitted: 2014-08-04 12:31 UTC Modified: 2015-01-09 15:15 UTC
From: php at tutteli dot ch Assigned:
Status: Wont fix Package: PHP Language Specification
PHP Version: 5.5.15 OS:
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: php at tutteli dot ch
New email:
PHP Version: OS:

 

 [2014-08-04 12:31 UTC] php at tutteli dot ch
Description:
------------
PHP allows to define multiple default cases in a switch. According to the spec this should not be allowed:
https://github.com/php/php-langspec/blob/master/spec/11-statements.md#the-s
witch-statement


Test script:
---------------
<?
$a=1;
$b=0;
switch($a) {
  default:
    $b += 1;
    break;
  default:
    $b += 2;
}
echo $b; //output would be 2
?>

Expected result:
----------------
parser syntax error


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-05 19:19 UTC] aharvey@php.net
-Package: *General Issues +Package: PHP Language Specification
 [2014-08-05 19:19 UTC] aharvey@php.net
This is probably an issue with the spec, then: AIUI, the spec should be documenting what 5.6 does at present.
 [2014-08-05 23:18 UTC] pollita@php.net
I'd argue that we can look at this either way.

It's kind of silly to allow the definition of two defaults in a switch statement, so the question is: Do we define silliness as normal and update the spec, or do we fix a weirdness in the implementation by updating the engine.

I for one, vote for the latter.
 [2014-08-05 23:21 UTC] ajf@php.net
FWIW, I'm also in favour of fixing implementations here.
 [2014-08-05 23:32 UTC] joelm@php.net
+1 on the spec being the optimal behavior. Don't allow this. PHP5 and HHVM actually differ on behavior here, scarily enough. PHP5 executes the code in the last default. HHVM executes the code in the first default.
 [2014-08-13 11:32 UTC] tyrael@php.net
I would argue, that the correct output should be 1.
If you replace default with case 1, that is what the output will be, as both of the cases would match in the order of definition, but after the first case we would break out so the second case would never got a chance to be executed.
What's even more interesting is that even the default blocks work this way if there is a normal case block which matches the value in the switch.
here is an example for the original snippet from the bugreport:
http://3v4l.org/9RtL8
here is one for the same but using matching case vs default:
http://3v4l.org/Ioqsq
and here is an example where the defaults behave differently if there is a previous matching case:
http://3v4l.org/r3poR

so I think that the hhvm behavior is correct and the current php behavior is wrong, but this isn't a design problem of the support of multiple defaults but an implementation problem, where we don't properly iterate over the cases from top to bottom.
 [2014-08-13 13:51 UTC] tyrael@php.net
nevermind me, I'm just an idiot. /o\
 [2015-01-09 15:15 UTC] jpauli@php.net
-Status: Open +Status: Wont fix
 [2015-01-09 15:15 UTC] jpauli@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I close with won't fix, as we now have an RFC for this case, and a PR as well
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 02:01:29 2024 UTC