Bug #54547 [Com]: wrong equality of string numbers

From: Date: Thu, 12 Apr 2012 05:42:53 +0000
Subject: Bug #54547 [Com]: wrong equality of string numbers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-169468@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54547&edit=1

 ID:                 54547
 Comment by:         a at hotmail dot com
 Reported by:        peter dot ritt at gmx dot net
 Summary:            wrong equality of string numbers
 Status:             Verified
 Type:               Bug
 Package:            Unknown/Other Function
 Operating System:   linux
 PHP Version:        5.3.6
 Assigned To:        dmitry
 Block user comment: N
 Private report:     N

 New Comment:

@pajoye@php.net

How about *you* staying polite with your users by actually fixing your bugs? Can you imagine how
much time is wasted worldwide as a consequence of your shortsighted "design" decisions?


Previous Comments:
------------------------------------------------------------------------
[2012-04-11 13:36:46] pajoye@php.net

@nik at naturalnet dot de

Please stay polite with other developers.

Please keep in mind that PHP is loosely typed, this is a root principle of PHP.

------------------------------------------------------------------------
[2012-04-11 13:12:14] nik at naturalnet dot de

*Why* the heck is that implicit cast even done?

Are PHP developers really _that_ absent-minded that they cannot write actual number literals when
they want them (i.e. leave out the '')?

I expect any programming language to use the data types I give it, not something it likes more!

------------------------------------------------------------------------
[2012-04-11 10:52:06] hholzgra@php.net

On "0xFF" == 255:

since when do we actually consider hex in strings as numeric?
And is this actually documented?

The 
The "String conversion to numbers" section in the manual says:

"Valid numeric data is an optional sign, followed by one or more digits (optionally containing
a decimal point), followed by an optional exponent. The exponent is an 'e' or
'E' followed by one or more digits."

( 
http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
 )

By that description 0xsomething would *not* be considered
as numeric in a string context ...

------------------------------------------------------------------------
[2012-04-11 10:45:46] hholzgra@php.net

If we indeed want to change the behavior here (and i'm yet undecided to whether i'd want
to do this or not, although slightly biased towards a 'yes):

wouldn't it be easier (although probably slightly less effective performance wise) to do a
string comparison first if both arguments are strings, and only fall back to numeric auto casts if
the string comparison fails?

If the strings really contain different numeric literals i'd expect a string comparison to fail
quickly as there can only be so much digits (ok, in theory you could have up to 300+ digits, but not
all of them significant).

This would take care of all possible edge cases (assuming that there may be others that we
aren't aware of yet, even though i can't think of any right now) and not just the overflow
case at hand, and the required engine changes would probably be a single chunk only, so having
better patch locality ...

Or are there other places where we'd need an extended is_numeric_string check with overflow
control, too?

------------------------------------------------------------------------
[2012-04-11 08:44:39] net at janoszen dot hu

Same problem:

php > var_dump('0xff' == '255');
bool(true)

------------------------------------------------------------------------


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

    https://bugs.php.net/bug.php?id=54547


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=54547&edit=1


Thread (44 messages)

« previous php.bugs (#169468) next »