Req #66361 [NEW]: Risky behavior of switch statement
| From: | mmshfe at gmail dot com | Date: | Fri, 27 Dec 2013 20:37:55 +0000 |
| Subject: | Req #66361 [NEW]: Risky behavior of switch statement | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-183473@lists.php.net to get a copy of this message | ||
From: mmshfe at gmail dot com
Operating system: Any OS
PHP version: Irrelevant
Package: SPL related
Bug Type: Feature/Change Request
Bug description:Risky behavior of switch statement
Description:
------------
As mentioned in docs, switch statement is a loose type comparison tool.
But this may be lead to a serious bug in user products. It would be
really better to force it to use strong type comparison (or have an
option such as an optional 2nd argument let the users to do so). In the
real world, we have no other loose type languages that behaves like
this. For example, in JavaScript, the result is fine. I mean, although
being a loose type language is a good feature for PHP in overall; It
should not be led to unexpected results those not shown in any other
language (even the other loose type ones).
I really now the process flow. It uses if, elseif, else statements like
this:
if($v == 'hello') {
echo 'Hello';
}
elseif($v == 'goodbye') {
echo 'Goodbye';
}
else {
echo 'Error';
}
And 'hello' is converted to integer (with intval('hello') or anything
similar) and because it does not contain an integer value at the
beginning, it uses the default integer value (zero) and so, the first
case becomes true. All I want to say is that this behavior is incorrect
because approximately always it's not the behavior that the developer
expected.
Test script:
---------------
$v = 0;
switch($v) {
case 'hello':
echo 'Hello';
break;
case 'goodbye':
echo 'Goodbye';
break;
default:
echo 'Error';
break;
}
Expected result:
----------------
Error
Actual result:
--------------
Hello
--
Edit bug report at https://bugs.php.net/bug.php?id=66361&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66361&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66361&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66361&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66361&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66361&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66361&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66361&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66361&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66361&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66361&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66361&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66361&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66361&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66361&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66361&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66361&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66361&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66361&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66361&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66361&r=mysqlcfg