|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79079 Opt-in strict [and/or custom] comparison for switch/case
Submitted: 2020-01-08 11:09 UTC Modified: 2020-01-08 20:58 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: phpuser at yopmail dot com Assigned: cmb (profile)
Status: Duplicate Package: Scripting Engine problem
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
 [2020-01-08 11:09 UTC] phpuser at yopmail dot com
First, switch/case compares values using *loose* equality (==) which causes issues with null, 0, "" etc. and requires ugly hacks, for instance:

I almost always want *strict* equality (===).

Idea of syntax (related to the second idea below):

    switch ( EXPR ) using strict_equality { CASES }

(note: that syntax is open to debate, but in any case I would *not* want a declare()).

Second (but far less important), sometimes I would like to use a switch/case with a custom comparison (instead of equality).

Idea of syntax (related to the first idea above):

    switch ( EXPR ) using ( BINARY_PREDICATE ) { CASES }


    switch (getSomeString()) using ('str_starts_with') {
        case 'foo_':
        case 'bar_':
            /* starts with "foo_" or "bar_" */
        case 'qux_2_':
            /* starts with "qux_2_" */
            // no break
        case 'qux_':
            /* starts with "qux_" */
            /* doesn't start with any of the above */

    switch (getSomeString()) using (fn($value, $case) => (bool) preg_match($case, $value)) {
        case '/^(0|-?[1-9]\d*)$/D':
            /* ... */
        case '/^[[:xdigit:]]{2}$/D':
            /* ... */
        // ...

    switch (getSomeInt()) using (fn($value, $case) => $value >= $case) {
        case 50:
            /* >= 50 (i.e. in range(50, \PHP_INT_MAX)) */
        case 10:
            /* in range(10, 49) */
        case 0:
            /* in range(0, 9) */
            /* < 0 (i.e. in range(\PHP_INT_MIN, -1)) */

And as a bonus, it would be even nicer if it could handle specifying built-in comparison operators directly like e.g. "using (>=)" (or maybe "using >="), in which case maybe "using strict_equality" wouldn't be needed.

But again, let's not let extra wishes hinder the most wanted: strict switch.



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-08 11:20 UTC]
-Status: Open +Status: Duplicate -Package: PHP Language Specification +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2020-01-08 11:20 UTC]
That looks like a duplicate of bug #70440.
 [2020-01-08 20:10 UTC] phpuser at yopmail dot com
@cmb: The linked request (whose syntax is swapped and example buggy, btw) had been created more than 4 years ago, and was suspended more than 2 years ago (on the motive that it would need an RFC, but given how proposals from outsiders are generally "welcomed" on internals, I'm not very surprised that nobody has tried since then)... so what now?
 [2020-01-08 20:58 UTC]
The idea isn't dead, we're just marking it here on this bug tracker as a duplicate - meaning there is something here on the tracker that addresses the issue. And the age of it really isn't that important.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Dec 03 11:01:27 2023 UTC