php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39901 casting of string with numbers in it still does INT conversion
Submitted: 2006-12-20 13:16 UTC Modified: 2006-12-20 16:28 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: schizoduckie at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5CVS-2006-12-20 (snap) OS: Win32/XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: schizoduckie at gmail dot com
New email:
PHP Version: OS:

 

 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-20 13:20 UTC] 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:25 UTC] 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:34 UTC] 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:43 UTC] 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 16:19 UTC] schizoduckie at gmail dot com
I have had a little discussion here, in dutch (http://gathering.tweakers.net/forum/list_messages/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 16:28 UTC] schizoduckie at gmail dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 17:01:33 2024 UTC