php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70440 Support strict switch
Submitted: 2015-09-06 18:34 UTC Modified: 2017-08-05 04:47 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: david dot proweb at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-09-06 18:34 UTC] david dot proweb at gmail dot com
Description:
------------
Currently switch() are loose (as said on doc), but it should be strict by using a keyword. It's need to avoid somekind of hacks:

Suggestion:

    strict switch ($value) ...
    switch strict ($value) ...

Or, is possible improve a lot the strict by accept an operator. It should compare the switch value with case value:

    switch == $value ... or 
    switch == ($value) ...

    switch === $value ...
    switch >= $value ...
    switch < $value ...

So you can do something like that:

    switch >= $strength {
        case 0: return 'weak';
        case 40: return 'medium';
        case 80: return 'strong';
        case 100: return 'perfect';
    }

Currently it's possible by 'hacking' that:

    switch (true) {
        case $strength >= 0: return 'weak';
        case $strength >= 40: return 'medium';
        case $strength >= 80: return 'strong';
        case $strength >= 100: return 'perfect';
    }



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-25 19:17 UTC] dave at mudsite dot com
Switch()'s take inputs and let you have cases for single option of the element.  To preform ranges I'm not certain a switch() would be the right idea.  I'm not sure having the 'hacked' switch is any better, or easier to read than:

if ($strength >= 0) return 'weak';
elseif ($strength >= 40) return 'medium';
elseif ($strength >= 80) return 'strong';
elseif ($strength >= 100) return 'perfect';

^^^ is easier to read imo, and doesn't distort what switch() should be doing.
 [2017-02-08 09:21 UTC] arossi dot gweb at gmail dot com
A strict switch operator would be very useful to check for functions that return mixed integers and booleans. Example:

            switch (a_function_that_returns_int_or_bool()) {
                case 2:
                    // do something
                    break;
                case 3:
                    // do something
                    break;
                case true:
                default:
                    // do something
                    break;
            }

This switch statement always evaluates to the first case if the return value of the function is (bool) true. The same happens if you put the "case true" as the first case.

We really need a "strict switch".
 [2017-08-05 04:47 UTC] stas@php.net
-Status: Open +Status: Suspended
 [2017-08-05 04:47 UTC] stas@php.net
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced developers. 

Please to not consider this comment as a negative view on the merits of your proposal - every proposal which requires changes of certain magnitude, even the very successful and widely supported ones, must be done through the RFC process. This helps make the process predictable, transparent and accessible to all developers.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC