Req #70440 [Com]: Support strict switch
Edit report at https://bugs.php.net/bug.php?id=70440&edit=1
ID: 70440
Comment by: dave at mudsite 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:
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.
Previous Comments:
------------------------------------------------------------------------
[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)