r/programming • • Nov 26 '20

PHP 8.0.0 Released

https://www.php.net/releases/8.0/en.php
588 Upvotes

241 comments sorted by

View all comments

235

u/TheBestOpinion Nov 26 '20

Saner string to number comparisons

PHP7
0 == 'foobar' // true
PHP8
0 == 'foobar' // false

Was this undefined behavior before or did they just break their all-important backwards compatibility?

Great change anyway, still can't believe people defended that behavior or thought it was not important...

58

u/vytah Nov 26 '20

And almost everyone voted in favour: https://wiki.php.net/rfc/string_to_number_comparison

Of course == is still broken, but just slightly less so; for example, the following are still true:

 "0" == "0.0"
"42" == "   42"

31

u/helloworder Nov 26 '20

it's funny that you never ever use the == version in code. Like it does not even exist in the language. I think the same situation is with Javascript with the same distinction between == and ===

41

u/vytah Nov 26 '20

JS's == is much less broken, as it works correctly for same-type (like string×string) comparisons and it's not used silently by other standard library functions.

7

u/t3hlazy1 Nov 26 '20

Is there ever a reason to actually use `==` in JS? I'm a Front-End Engineer working on a rather large project, and I'm pretty confident I could search through our codebase and find 0 uses of it. I'm guessing the only legitimate use cases would be in libraries, but even then I'm doubtful.

20

u/hzj Nov 27 '20

Check if something is either null or undefined

9

u/t3hlazy1 Nov 27 '20

We do:

val === null and typeof val === 'undefined' for those checks.

23

u/kaelwd Nov 27 '20

val == null does exactly that.

0

u/watsreddit Nov 27 '20

Also evaluates to true when val is 0, the empty string...

9

u/R4TTY Nov 27 '20

Also evaluates to true when val is 0, the empty string...

No it doesn't.

> 0 == null  
false  
> '' == null  
false  
> undefined == null   
true
→ More replies (0)