#50738 [Opn]: Unclear documentation in numerical string comparison
| From: | rquadling@php.net | Date: | Wed, 13 Jan 2010 11:14:50 +0000 |
| Subject: | #50738 [Opn]: Unclear documentation in numerical string comparison | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-3693@lists.php.net to get a copy of this message | ||
ID: 50738
Updated by: rquadling@php.net
Reported By: I dot F dot A dot C dot Fokkema at LUMC dot nl
Status: Open
Bug Type: Documentation problem
Operating System: All
PHP Version: Irrelevant
New Comment:
How about ...
"If you compare an integer value with a string value (as opposed to
comparisons including type; === and !===), ..."
Previous Comments:
------------------------------------------------------------------------
[2010-01-13 11:07:06] I dot F dot A dot C dot Fokkema at LUMC dot nl
Description:
------------
Hi guys,
After someone filed bug report #23110, the documentation was greatly
improved to emphasize pitfalls when comparing numerical strings.
However, I want to suggest an addition because in my point of view it's
not clear enough.
== and === compare numerical strings differently:
echo (1 == "1e0") // Returns true, as documented
echo (1 === "1e0") // Returns false as expected, because the types are
different
echo ("1" == "1e0") // Returns true, as documented
echo ("1" === "1e0") // Returns false, but the documentation says
otherwise
The documentation at
http://www.php.net/manual/en/language.operators.comparison.php
currently
reads:
"If you compare an integer with a string, the string is converted to a
number. If you compare two numerical strings, they are compared as
integers. These rules also apply to the switch statement." - this was
added after bug report #23110. So according to this, ("1" === "1e0")
should return true; the converted numbers are equal and they're of the
same type. However, it returns false because the number conversion
described in the manual does not happen when using === or !==, but that
is not documented. More people have issues with this, as can be seen
looking through the comments on the aforementioned manual page.
Thus, I would like to suggest an addition at the end:
"(...) These rules also apply to the switch statement, but they do not
apply when using the operators === or !==, in which case there is no
type conversion used." Or a similar explanation of course.
Thank you all for your great work!
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50738&edit=1