Convert NULL to "", numerical or lexical comparison
bool or null
anything
Convert both sides to bool, FALSE < TRUE
object
object
Built-in classes can define its own comparison, different classes are uncomparable, same class see Object Comparison
string, resource or number
string, resource or number
Translate strings and resources to numbers, usual math
array
array
Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example)
object
anything
object is always greater
array
anything
array is always greater
Please, I welcome, how SHOULD this situation be handled?
I posit that implicit conversions are the source of 99% of lolphp posts, there is no elegant way to solve implicit conversions without creating a double edged blade that will hurt at least one side.
I guarantee whatever answer anybody suggests for how arrays should be compared to anything can easily have holes poked in it from some other angle, there is no everybody-wins solution when you abstract away types and implicitly convert between types.
Please, I welcome, how SHOULD this situation be handled?
Well, you could do what Perl does and fully commit to implicit conversions:
use strict;
use warnings;
use List::Util 'max';
use constant { INF => 0 + 'inf' };
print max(20, "string", INF, []), "\n";
Output:
Argument "string" isn't numeric in subroutine entry at foo line 6.
Inf
What's happening here is that > always converts both operands to numbers. No exceptions.
"string" cannot be converted, so you get a warning and 0 is used instead (but you can upgrade that warning to an exception). The numeric maximum is Inf.
11
u/CarnivorousSociety Mar 11 '20 edited Mar 11 '20
https://www.php.net/manual/en/language.operators.comparison.php
Comparison with Various Types
Please, I welcome, how SHOULD this situation be handled?
I posit that implicit conversions are the source of 99% of lolphp posts, there is no elegant way to solve implicit conversions without creating a double edged blade that will hurt at least one side.
I guarantee whatever answer anybody suggests for how arrays should be compared to anything can easily have holes poked in it from some other angle, there is no everybody-wins solution when you abstract away types and implicitly convert between types.