|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-07-14 16:46 UTC] phpbug at bart dot w-wa dot pl
Description:
------------
Due to FU typecasting switch produces unexpected results when one of the cases is string, but variable passed to switch is int.
Reproduce code:
---------------
$x = 0;
switch($x) {
case 'something': echo 'something'; break;
case 0: echo 'zero'; break;
default: echo 'something else';
};
Expected result:
----------------
zero
Actual result:
--------------
something
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 09:00:02 2025 UTC |
I am aware _why_ this happens, but still I do belive it's a flaw. Perhaps a flaw in design, not the code itself. This odd behavior is unique to PHP. In other languages, where == is used both for int and string comparison 0 != 'something' (eg. JavaScript). In Perl 0 == 'something' only becouse == is an operator for _numeric_ comparison, while strings are compared with eq operator. BTW, the example "works" other way around too: $x = 'something'; switch($x) { case 0: echo 'wrong'; break; case 'something': echo 'ok'; break; };