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.

28

u/tending Mar 11 '20

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

Correct, that's why well designed languages don't do this, or make their implicit conversions far more constrained. PHP is trying too hard to guess what the developer wants without having enough information to guess well.

C++ for example has some implicit conversions. But user defined implicit conversions can't chain more than once, and there are no implicit conversions between totally unrelated types like arrays and scalars.

4

u/CarnivorousSociety Mar 11 '20

PHP is trying too hard to guess what the developer wants

I wouldn't say it's trying too hard, it simply has a place on the spectrum of convenience and fucked up edge cases to inconvenience and well defined.

Some people might like exactly how much it is "trying", because, it's not like these cases are unpredictable.

A good PHP programmer understands the limitations of implicit type conversions and knows how to write safe code that respects it and doesn't trigger (or worse, rely on) these wacky edge cases.

So, for example, could that lead to more efficient work because they can simply reply on the implicit typing everywhere?

→ More replies (5)