php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50738 Unclear documentation in numerical string comparison
Submitted: 2010-01-13 11:07 UTC Modified: 2010-01-13 15:08 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: I dot F dot A dot C dot Fokkema at LUMC dot nl Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: I dot F dot A dot C dot Fokkema at LUMC dot nl
New email:
PHP Version: OS:

 

 [2010-01-13 11:07 UTC] 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!


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-13 11:14 UTC] rquadling@php.net
How about ...

"If you compare an integer value with a string value (as opposed to 
comparisons including type; === and !===), ..."
 [2010-01-13 13:25 UTC] svn@php.net
Automatic comment from SVN on behalf of rquadling
Revision: http://svn.php.net/viewvc/?view=revision&revision=293501
Log: Fix#50738 - Type juggling is excluded from identical and not identical comparisons.

"If you compare an integer with a string, excluding the identical (===) and not identical (!===) operators, the string is ..."
 [2010-01-13 13:31 UTC] rquadling@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2010-01-13 13:53 UTC] I dot F dot A dot C dot Fokkema at LUMC dot nl
I hope I'm not bothering you too much now, but do you think people will understand the conversion also doesn't take place when comparing two numerical strings? Now it seems to me it's affecting only the integer-to-string comparison...
By the way, !=== needs to be !== :)
 [2010-01-13 14:49 UTC] rquadling@php.net
Also, 2 strings are NOT compared as "integers" ...

php -r "var_dump('123.4'=='1.234e2');"

outputs

bool(true)


So ...

"If a comparison involves numerical strings, then each string in the 
comparison is converted to a number and then the comparison is 
performed numerically. The type conversion does not take place when 
the comparison is === or !== as these involve comparing the type as 
well as the value. These rules also apply to the switch statement."


I think covers it all and the correction with the word "integers".

 [2010-01-13 15:08 UTC] I dot F dot A dot C dot Fokkema at LUMC dot nl
Almost... :) Now it doesn't include anymore that if one is a normal string and the other is a number (int/float) that the string will be converted.
Because ('a' == 0 && 'a' == 0.0) !
Keeping the first sentence (although changing int=>number) and add it to your new text should suffice, don't you think? So how about:
"If you compare a number with a string, the string is converted to a
number. If a comparison involves numerical strings, then each string in the comparison is converted to a number and then the comparison is 
performed numerically. The type conversion does not take place when 
the comparison is === or !== as these involve comparing the type as 
well as the value. These rules also apply to the switch statement."
 [2010-01-13 16:12 UTC] svn@php.net
Automatic comment from SVN on behalf of rquadling
Revision: http://svn.php.net/viewvc/?view=revision&revision=293507
Log: Better fix for #50738
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 08:01:29 2024 UTC