|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81398 Comparing int/string with max() always returns string
Submitted: 2021-08-29 21:57 UTC Modified: 2021-08-30 09:04 UTC
From: php at ober-mail dot de Assigned:
Status: Verified Package: *Math Functions
PHP Version: 8.0.10 OS: Debian 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
45 + 15 = ?
Subscribe to this entry?

 [2021-08-29 21:57 UTC] php at ober-mail dot de
The documentation of max() says:

"Values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an int as though it were 0."

Using PHP 8.0 and above, I'm observing a deviating behavior, that is, the string value is returned instead. Parameter order does not matter. If this is the intended behavior, the documentation should be updated / the cited snippet should be removed.

Up to PHP 7.4.13, the observed result matches the documentation.

Test script:

var_dump(max("A", PHP_INT_MAX));
var_dump(max(PHP_INT_MAX, "A"));

Expected result:

Actual result:
string(1) "A"
string(1) "A"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-29 21:58 UTC] php at ober-mail dot de
I'm sorry, of course the "Test script" should be:


var_dump(max("A", 1));
var_dump(max(1, "A"));
 [2021-08-29 22:38 UTC]
The next sentence is: "The actual value returned will be of the original type with no conversion applied."

Which seems quite clear to me.
 [2021-08-29 22:45 UTC] 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.
 [2021-08-29 22:48 UTC] 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:50 UTC]
"will be COMPARED...The actual value RETURNED will be of the original type".
 [2021-08-29 23:32 UTC]
-Status: Open +Status: Verified
 [2021-08-29 23:32 UTC]
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.

 [2021-08-29 23:32 UTC]
-Type: Bug +Type: Documentation Problem
 [2021-08-30 08:23 UTC] 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-30 09:04 UTC]
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.
 [2022-12-19 05:28 UTC] samira dot akhlaqi314 at gmail dot com
The actual value returned will be of the original type with no conversion applied.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 09:01:32 2024 UTC