|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-12-20 13:16 UTC] schizoduckie at gmail dot com
Description:
------------
In a switch, a value that is declared as string with a prefix 0 will automatically disappear in switched values, even if all of them are specifically casted as a string.
Reproduce code:
---------------
$field = (string)"0123";
settype($field, 'string'); // just to make sure, for testcase
switch ($field)
{
case '123': $result = 'first'; break;
case '456': $result = 'second'; break;
default: $result = 'third'; break;
}
echo $result; // first, due to string / int conversion.
switch ((string)$field) // explicit cast to string
{
case (string)'123': $result = 'first'; break; // more (unneccesary casts just to be sure)
case (string)'456': $result = 'second'; break;
default: $result = 'third'; break;
}
echo $result; // first! ????
Expected result:
----------------
I would have expected the last result echo to be third because of the explicit typecasting.
This problem appears in all php versions up to 5.2 snapshot of today.
Actual result:
--------------
the code in the example speaks for itself.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 19:00:01 2025 UTC |
switch() construct uses "==" semantics to compare values, which compares numeric strings as numbers: var_dump("0123"=="123"); => true If you want to compare numeric strings as strings, use "===": var_dump("0123"==="123"); => false. This is expected behaviour.