Req #70440 [Com]: Support strict switch

From: Date: Wed, 08 Feb 2017 09:21:32 +0000
Subject: Req #70440 [Com]: Support strict switch
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207224@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70440&edit=1

 ID:                 70440
 Comment by:         arossi dot gweb at gmail dot com
 Reported by:        david dot proweb at gmail dot com
 Summary:            Support strict switch
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 PHP Version:        Next Minor Version
 Block user comment: N
 Private report:     N

 New Comment:

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".


Previous Comments:
------------------------------------------------------------------------
[2016-07-25 19:17:38] 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.

------------------------------------------------------------------------
[2015-09-06 18:34:01] 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';
    }




------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=70440&edit=1


Thread (4 messages)

« previous php.bugs (#207224) next »