|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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';
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 22:00:01 2025 UTC |
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".