Sitelet https://github.com/php/doc-en/pull/5914
Skip to content

Fix outdated string to number comparison rules in min() and max() - #5914

Merged
lacatoire merged 2 commits into
php:masterfrom
angeliszotis:doc/min-max-php8-comparison
Oct 7, 2026
Merged

lacatoire merged 2 commits into
php:masterfrom
angeliszotis:doc/min-max-php8-comparison

Conversation

@angeliszotis

Copy link
Copy Markdown
Contributor

Fixes #2687

The issue is about one comment in the max() example, but while checking it I ran every claim and every example on both pages against PHP 8.4.26, and there are four things that still describe the pre 8.0 semantics.

1. The note in the description, on both pages

For instance, a non-numeric string will be compared to an int as though it were 0

Since PHP 8.0 it works the other way around. The int is cast to string and the values are compared as strings:

var_dump("hello" == 0);      // bool(false)
var_dump("hello" <=> 0);     // int(1)
var_dump("hello" <=> "0");   // int(1)   same result, the int became a string

2. The return values section, on both pages

If multiple values of different types evaluate as equal (e.g. 0 and 'abc')

var_dump(0 == "abc");   // bool(false)

I replaced it with 0 and false, which still evaluate as equal, and the rule itself still holds:

var_dump(0 == false);     // bool(true)
var_dump(max(0, false));  // int(0)
var_dump(max(false, 0));  // bool(false)

3. The 'hello' example, on both pages — this is the one from the issue

// Here we are comparing -1 < 0, so 'hello' is the highest value

The result is still correct, but not for that reason:

var_dump(max("hello", -1));     // string(5) "hello"   correct
var_dump("hello" == 0);         // bool(false)         the stated reason is not
var_dump("hello" <=> "-1");     // int(1)              "h" (104) > "-" (45)

4. A documented output in min() that is now wrong

$val = min('string', array(2, 5, 7), 42);   // string
var_dump(min("string", array(2,5,7), 42));   // int(42)

It returns 42. The array is still never returned, that part of the comment is correct, but of the two remaining values 42 is cast to the string "42" when compared with the non-numeric string 'string', and "42" is lower than "string". Before 8.0 'string' became 0, so 0 < 42 and 'string' was the lowest.

What I did not change

I ran the other fourteen examples on both pages, the arrays, the booleans and NULL ones, and the basic usages. They all still produce what the manual says, so I left them alone.

Worth noting

The changelog on both pages already says:

8.0.0 — As string to number comparisons were changed, max() no longer returns a different value based on the argument order in those cases.

So the pages already acknowledge the change, only the description, the return values and the examples were never updated to match.

Both pages still describe the comparison semantics from before PHP 8.0.
I checked every claim and every example on PHP 8.4.26, these are the
ones that no longer hold:

The note in the description says a non-numeric string is compared to an
int "as though it were 0". Since PHP 8.0 it is the other way around, the
int is cast to string and the comparison is done as strings.

The return values section uses 0 and 'abc' as an example of two values
of different types that evaluate as equal. In PHP 8, 0 == 'abc' is
false. Replaced with 0 and false, which is still true, and the order
still decides which one is returned.

The 'hello' example explains the result with "comparing -1 < 0". The
result is still 'hello' but for a different reason, -1 becomes the
string "-1" and "hello" is greater than it as a string.

In min(), the documented output of min('string', array(2, 5, 7), 42) is
'string'. It returns 42 now, because 42 is compared with 'string' as the
string "42", and "42" is lower than "string".

The changelog on both pages already mentions that string to number
comparisons changed in 8.0, so the pages contradict themselves.

Fixes php#2687
@lacatoire

Copy link
Copy Markdown
Member

Thanks @angeliszotis, few linter nits, but LGTM

The DocBook style check flagged the note and return-value paragraphs.
@angeliszotis

Copy link
Copy Markdown
Contributor Author

Thanks @lacatoire! Converted the inline-only paragraphs to simpara to clear the style check.

@lacatoire
lacatoire merged commit 9b77252 into php:master Oct 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Min()/Max() example has incorrect information comparing with "hello"

2 participants