Repository navigation
Fix outdated string to number comparison rules in min() and max() - #5914
Merged
Merged
Conversation
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
Member
|
Thanks @angeliszotis, few linter nits, but LGTM |
The DocBook style check flagged the note and return-value paragraphs.
Contributor
Author
|
Thanks @lacatoire! Converted the inline-only paragraphs to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Since PHP 8.0 it works the other way around. The int is cast to string and the values are compared as strings:
2. The return values section, on both pages
I replaced it with
0andfalse, which still evaluate as equal, and the rule itself still holds:3. The
'hello'example, on both pages — this is the one from the issueThe result is still correct, but not for that reason:
4. A documented output in
min()that is now wrongIt returns
42. The array is still never returned, that part of the comment is correct, but of the two remaining values42is cast to the string"42"when compared with the non-numeric string'string', and"42"is lower than"string". Before 8.0'string'became0, so0 < 42and'string'was the lowest.What I did not change
I ran the other fourteen examples on both pages, the arrays, the booleans and
NULLones, 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:
So the pages already acknowledge the change, only the description, the return values and the examples were never updated to match.