|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-08-29 21:57 UTC] php at ober-mail dot de
Description:
------------
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:
---------------
<?php
var_dump(max("A", PHP_INT_MAX));
var_dump(max(PHP_INT_MAX, "A"));
Expected result:
----------------
int(1)
int(1)
Actual result:
--------------
string(1) "A"
string(1) "A"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 22:00:02 2025 UTC |
I'm sorry, of course the "Test script" should be: <?php var_dump(max("A", 1)); var_dump(max(1, "A"));@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)?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.