Edit report at https://bugs.php.net/bug.php?id=54547&edit=1
ID: 54547
Comment by: four dot zero dot one dot unauthorized at gmail 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:
This behavior is documented here:
http://php.net/manual/en/language.operators.comparison.php
"If you compare a number with a string or the comparison involves numerical
strings, then each string is converted to a number and the comparison performed
numerically. These rules also apply to the switch statement. The type
conversion does not take place when the comparison is === or !== as this
involves comparing the type as well as the value. "
Shouldn't this feature of converting numerical strings to numbers during loose
comparison operations between two strings be dropped? If a developer wanted to
compare values given during POST or GET processing AS numbers, they should cast
the inputs to (int) or (float) first. There really should be a fundamental
shift away from catering to developer laziness, and force developers to pay
more attention to variable and input handling on their own.
Previous Comments:
------------------------------------------------------------------------
[2012-04-13 01:10:44] the dot matt dot kantor at gmail dot com
@hholzgra: Your only-coerce-on-failure proposal would not solve this issue.
Assuming that by "fail" you mean "the comparison evaluates to false", the
strings would end up being coerced anyway (since they are indeed different),
they'd become identical floats, and things would be the same as they are now.
If I misunderstood what you meant by "fail", then we'd lose "1" == "1.0", which
I don't think is something that can (or should) happen.
------------------------------------------------------------------------
[2012-04-12 22:45:28] erowid at inbox dot lv
I want to marry it, lather this thread up, and have my way with it. I want to
have little threads everywhere that are as funny as this xD
------------------------------------------------------------------------
[2012-04-12 22:14:36] chx1975 at gmail dot com
Now, while I can understand why PHP chooses "1" == 1 (HTML, sure) I am not too
sure how is that relevant when both sides are strings?? I am not quite sure why
the strings "1" and "1.0" would need to be ==. Just because "1" == 1 and "1.0"
==
1 does not mean "1" == "1.0". It's not transitive! Compare FALSE == 0; 0 ==
'x';
'x' == TRUE -- if it would be transitive then FALSE == TRUE, surely you don't
want that.
------------------------------------------------------------------------
[2012-04-12 21:23:40] vinny_182 at hotmail dot com
Equality is equality and neither string or numeric representations of the value
are equal. The bug IMO is in the conversion from string to float, the
conversion
has failed but a valid value is still returned. That's just plain wrong. If you
wrote unit tests for string to float conversions and this was the input you
would
expect it to return a null value or throw an exception.
------------------------------------------------------------------------
[2012-04-12 21:02:06] c at hotmail dot com
"In the majority of cases when dealing with HTTP requests and database results,
which is what PHP deals with most, the loose comparison makes life easiest on
the developer."
By 'the developer' I assume you mean people who can't type (string) or (int) ?
No other language has this issue because they aren't designed around
programmers who do not really understand how to program. Please make the
developer's life easier by making comparisons make sense.
------------------------------------------------------------------------
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