#50738 [NEW]: Unclear documentation in numerical string comparison

From: Date: Wed, 13 Jan 2010 11:07:06 +0000
Subject: #50738 [NEW]: Unclear documentation in numerical string comparison
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-3692@lists.php.net to get a copy of this message
From: I dot F dot A dot C dot Fokkema at LUMC dot nl Operating system: All PHP version: Irrelevant PHP Bug Type: Documentation problem Bug description: Unclear documentation in numerical string comparison 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 bug report at http://bugs.php.net/?id=50738&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=50738&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=50738&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=50738&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=50738&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50738&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=50738&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=50738&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=50738&r=needscript Try newer version: http://bugs.php.net/fix.php?id=50738&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=50738&r=support Expected behavior: http://bugs.php.net/fix.php?id=50738&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=50738&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=50738&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=50738&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50738&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=50738&r=dst IIS Stability: http://bugs.php.net/fix.php?id=50738&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=50738&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=50738&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=50738&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=50738&r=mysqlcfg

« previous php.doc.bugs (#3692) next »