#39901 [Opn->Bgs]: casting of string with numbers in it still does INT conversion

From: Date: Wed, 20 Dec 2006 13:43:06 +0000
Subject: #39901 [Opn->Bgs]: casting of string with numbers in it still does INT conversion
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-106641@lists.php.net to get a copy of this message
ID: 39901 Updated by: tony2001@php.net Reported By: schizoduckie at gmail dot com -Status: Open +Status: Bogus Bug Type: Variables related Operating System: Win32/XP PHP Version: 5CVS-2006-12-20 (snap) New Comment: It does not matter whether you cast the variables or not, switch() always uses semantics similar to "==". Again, there is nothing wrong and we're not going to change it. Previous Comments: ------------------------------------------------------------------------ [2006-12-20 13:34:25] schizoduckie at gmail dot com I disagree with this bug being bogus. Once you start giving people the power of casting, this type of programming should be either consistently be possible to use throughout the PHP or be completely removed to avoid any misunderstandings. ------------------------------------------------------------------------ [2006-12-20 13:25:29] schizoduckie at gmail dot com I agree that this is expected behavior for loosly typed variables. but i am CASTING explicity to String because i want it to be treated as string, which should trigger the switch to work as === ------------------------------------------------------------------------ [2006-12-20 13:20:45] tony2001@php.net 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. ------------------------------------------------------------------------ [2006-12-20 13:16:41] 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39901&edit=1

« previous php.bugs (#106641) next »