|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 12:00:02 2025 UTC |
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".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."