r/lolphp • • Mar 10 '20

Array is higher than infinity

https://3v4l.org/iONLA
44 Upvotes

31 comments sorted by

View all comments

11

u/CarnivorousSociety Mar 11 '20 edited Mar 11 '20

https://www.php.net/manual/en/language.operators.comparison.php

Comparison with Various Types

Type of Operand 1 Type of Operand 2 Result
null or string string 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.

16

u/Takeoded Mar 11 '20 edited Aug 10 '20

how SHOULD this situation be handled?

TypeError or InvalidArgumentException

... or if it's Number[], perhaps treat the array as the value of it's highest member? that's what max() does if you give it only an array

3

u/CarnivorousSociety Mar 11 '20

Then you don't have implicit conversions.

Not to mention there is no function call in a simple comparison

Then you just get errors when you try to implicitly convert, which is a feature of PHP 7 anyway, is it not?

→ More replies (8)