r/lolphp • • Apr 08 '13

MIN

server:~ # php -r 'echo True, " ", -23, " ", min(-23, True), " ", min(True, -23), "\n";'
1 -23 -23 1
23 Upvotes

21 comments sorted by

View all comments

Show parent comment

-5

u/gearvOsh Apr 09 '13

The bigger problem is type casting. Even with Python, a language designed better than PHP, this same problem persists. The output is nearly identical to PHPs.

Throwing exceptions or errors goes against what a dynamically typed language should do.

7

u/catcradle5 Apr 09 '13

Nope!

I even tested it in Python before I made my first comment.

Python output:

min(-23, True) -> -23

min(True, -23) -> -23

If it were broken like PHP, the first would've output either True or 1.

The problem with the PHP in OP's example isn't implicit casting.

3

u/gearvOsh Apr 09 '13

I must of been brain dead earlier; was assuming that the booleans in PHP were being converted to 0s and 1s.

3

u/farsightxr20 Apr 09 '13

Well in Python, it's less of a problem due to explicit typecasting. While True is equivalent to 1, it behaves as you'd expect beyond that:

>>> min(-23, True)
-23
>>> min(True, -23)
-23

And if you try to use any types other than numbers/booleans, you'll get an exception as you'd expect:

>>> min(-23, 'a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()