Req #70440 [Opn->Sus]: Support strict switch
Edit report at https://bugs.php.net/bug.php?id=70440&edit=1
ID: 70440
Updated by: stas@php.net
Reported by: david dot proweb at gmail dot com
Summary: Support strict switch
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: Next Minor Version
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2017-02-08 09:21:30] 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".
------------------------------------------------------------------------
[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)