Doc #81398 [Ver]: Comparing int/string with max() always returns string

From: Date: Mon, 30 Aug 2021 09:04:32 +0000
Subject: Doc #81398 [Ver]: Comparing int/string with max() always returns string
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19132@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81398&edit=1 ID: 81398 Updated by: salathe@php.net Reported by: php at ober-mail dot de Summary: Comparing int/string with max() always returns string Status: Verified Type: Documentation Problem Package: *Math Functions Operating System: Debian 10 PHP Version: 8.0.10 Block user comment: N Private report: N New Comment: When the page, currently, says, "a non-numeric string will be compared to an int as though it were 0" it is incorrect as of PHP 8.0.0. For your specific example of max(1, "A"), the 1 is (as of PHP 8.0.0) converted to a string ("1") then compared to "A" and the latter ("A") is the greater of the two. If your example was max(1, "/"), then the comparison would be of "1" and "/", in which case "1" is greater so the result would be 1. Previous Comments: ------------------------------------------------------------------------ [2021-08-30 08:23:21] php at ober-mail dot de @danack: Sorry, I still don't get it. If max(1, "A") is COMPARED like max(1, 0), why isn't int(1) returned? Isn't int(1) larger than string("A"), which is treated like int(0)? ------------------------------------------------------------------------ [2021-08-29 23:32:06] salathe@php.net This behaviour (comparing numbers and non-numeric strings) changed in PHP 8.0.0, resulting from the "saner string to number comparisons" RFC [1]. It is briefly mentioned as a backwards incompatible change in the migration guide for PHP 8.0.x [2]. The descriptions (in the comparison operators page [3], on the max() [4] and min() [5] pages, and all other relevant pages) has to be updated accordingly, if not done already. [1] https://wiki.php.net/rfc/string_to_number_comparison [2] https://www.php.net/manual/en/migration80.incompatible.php [3] https://www.php.net/manual/en/language.operators.comparison.php [4] https://www.php.net/manual/en/function.max.php [5] https://www.php.net/manual/en/function.min.php ------------------------------------------------------------------------ [2021-08-29 22:50:11] danack@php.net "will be COMPARED...The actual value RETURNED will be of the original type". ------------------------------------------------------------------------ [2021-08-29 22:48:14] php at ober-mail dot de Well, after reading it again, it does not seem clear at all. If a string is treated as int(0), why is the string returned in my example? Isn't 1 larger than 0? ------------------------------------------------------------------------ [2021-08-29 22:45:47] php at ober-mail dot de Well, it says "a non-numeric string WILL BE compared to an int as though it were 0." (emphasis mine). To my understanding, the sentence you cited is the general case (comparing, maybe, an object and a resource), whereas comparing int/string is a case with defined behavior. If comparing int/string is the same as comparing any other two different types, "will be" is not a good wording, IMHO. Better completely remove the sentence then, because it does not give any information. ------------------------------------------------------------------------ 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=81398 -- Edit this bug report at https://bugs.php.net/bug.php?id=81398&edit=1

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