![]() |
|
|
|
|
1
16th September 08:32
External User
Posts: 1
|
From: schizoduckie at gmail dot com
Operating system: Win32/XP PHP version: 5CVS-2006-12-20 (snap) PHP Bug Type: Variables related Bug description: casting of string with numbers in it still does INT conversion 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 bug report at http://bugs.php.net/?id=39901&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=39901&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=39901&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=39901&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=39901&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=39901&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=39901&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=39901&r=needscript Try newer version: http://bugs.php.net/fix.php?id=39901&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=39901&r=support Expected behavior: http://bugs.php.net/fix.php?id=39901&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=39901&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=39901&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=39901&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=39901&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=39901&r=dst IIS Stability: http://bugs.php.net/fix.php?id=39901&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=39901&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=39901&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=39901&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=39901&r=mysqlcfg |
|
|
|
|
2
16th September 08:33
External User
Posts: 1
|
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: 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. Previous Comments: ------------------------------------------------------------------------ [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 |
|
|
3
16th September 08:34
External User
Posts: 1
|
ID: 39901
User updated by: schizoduckie at gmail dot com Reported By: schizoduckie at gmail dot com Status: Bogus Bug Type: Variables related Operating System: Win32/XP PHP Version: 5CVS-2006-12-20 (snap) New Comment: 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 === Previous Comments: ------------------------------------------------------------------------ [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 |
|
|
4
16th September 08:34
External User
Posts: 1
|
ID: 39901
User updated by: schizoduckie at gmail dot com Reported By: schizoduckie at gmail dot com -Status: Bogus +Status: Open Bug Type: Variables related Operating System: Win32/XP PHP Version: 5CVS-2006-12-20 (snap) New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [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 |
|
|
5
16th September 08:35
External User
Posts: 1
|
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 |
|
|
6
16th September 09:49
External User
Posts: 1
|
ID: 39901
User updated by: schizoduckie at gmail dot com Reported By: schizoduckie at gmail dot com Status: Bogus Bug Type: Variables related Operating System: Win32/XP PHP Version: 5CVS-2006-12-20 (snap) New Comment: I have had a little discussion here, in dutch (http://gathering.tweakers.net/forum/...ssages/1185071) with some other programmers who also wanted to reply on this thread but couldn't because it's marked bogus. I'd just like this to be noted and added to the thread though: --- Quote -NME- --- Quite frankly, your excuse is kind of weak. Yes, switch uses the same semantics as the == operator. Yes, this means possible unwanted conversions (which, for the life of me, I don't understand - "0123" is a string; which, compared to "123" is _not_ the same. If I'd wanted it to be the same, I'd have defined it as 0123 and 123 respectively). But if you actually explicitly cast the variable to a string, I think it should do a strict comparison. PHP invites you to use strange code, very strange code indeed. For instance, the following returns true: PHP: <?php $a = 0; $b = "foo"; if ($a == $b) echo "PHP sucks"; ?> Idiotic inconsistencies like this one and the workarounds experienced programmers have to write just to make the language easy to work with for beginners really damage your reputation and the PHP language in general. Do with this comment as you please, but I really think you should provide experienced programmers with more means of keeping their code tidy and clean, without dumb workarounds. --- end quote -- Previous Comments: ------------------------------------------------------------------------ [2006-12-20 13:43:06] tony2001@php.net 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. ------------------------------------------------------------------------ [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 |
|
|
7
16th September 09:49
External User
Posts: 1
|
ID: 39901
User updated by: schizoduckie at gmail dot com Reported By: schizoduckie at gmail dot com Status: Bogus Bug Type: Variables related Operating System: Win32/XP PHP Version: 5CVS-2006-12-20 (snap) New Comment: In addition to the quote above: If the compiler/interpreter gets that if you cast a variable to a specific type, it could skip any standard typecasting being done and then "123" != "0123". The interpreter would not have to use === and the behavior would not have to be changed. Previous Comments: ------------------------------------------------------------------------ [2006-12-20 16:19:58] schizoduckie at gmail dot com I have had a little discussion here, in dutch (http://gathering.tweakers.net/forum/...ssages/1185071) with some other programmers who also wanted to reply on this thread but couldn't because it's marked bogus. I'd just like this to be noted and added to the thread though: --- Quote -NME- --- Quite frankly, your excuse is kind of weak. Yes, switch uses the same semantics as the == operator. Yes, this means possible unwanted conversions (which, for the life of me, I don't understand - "0123" is a string; which, compared to "123" is _not_ the same. If I'd wanted it to be the same, I'd have defined it as 0123 and 123 respectively). But if you actually explicitly cast the variable to a string, I think it should do a strict comparison. PHP invites you to use strange code, very strange code indeed. For instance, the following returns true: PHP: <?php $a = 0; $b = "foo"; if ($a == $b) echo "PHP sucks"; ?> Idiotic inconsistencies like this one and the workarounds experienced programmers have to write just to make the language easy to work with for beginners really damage your reputation and the PHP language in general. Do with this comment as you please, but I really think you should provide experienced programmers with more means of keeping their code tidy and clean, without dumb workarounds. --- end quote -- ------------------------------------------------------------------------ [2006-12-20 13:43:06] tony2001@php.net 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. ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/39901 -- Edit this bug report at http://bugs.php.net/?id=39901&edit=1 |
|
|
|
| Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php |