ID: 29155
User updated by: phpbug at bart dot w-wa dot pl
Reported By: phpbug at bart dot w-wa dot pl
Status: Bogus
Bug Type: Variables related
Operating System: Linux
PHP Version: 4.3.7
New Comment:
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;
};
Previous Comments:
------------------------------------------------------------------------
[2004-07-14 22:48:10] no at spam dot forme
you test one integer 0 against a string, which causes php to cast your
string into an integer, which results in 0 for the string 'something'.
thus, the first case comparison looks like 0 == 'something' <=> 0 == 0
<=> true, the program prints 'something' and breaks the switch. Your
result is the expected result. I think this should be more explicitly
empathized in the php manual.
------------------------------------------------------------------------
[2004-07-14 16:49:32] [EMAIL PROTECTED]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
.
------------------------------------------------------------------------
[2004-07-14 16:46:21] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=29155&edit=1