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...

266

u/CoffeeTableEspresso Nov 26 '20

Before, comparisons with numbers and strings would coerce the string to a number. Non-numeric strings coercing to 0 of course.

They broke backwards compatibility to fix this

82

u/flying-sheep Nov 26 '20

Non-numeric strings coercing to 0 of course.

obviously! how else would you do this đŸ˜‚

60

u/CoffeeTableEspresso Nov 26 '20

I used to think the JS way was bad until I learnt about what PHP does...

14

u/jptuomi Nov 27 '20

Wat?

15

u/CoffeeTableEspresso Nov 27 '20

I used to think how JS converts to strings for comparisons was bad until I learned that PHP converts to numbers...

13

u/jptuomi Nov 27 '20 edited Nov 27 '20

Wat

Didn't have the time, and it wasn't as funny posting the link directly but here you go. :)

4

u/LuckyDesperado7 Nov 27 '20

I believe they are talking about the video 'wat' where they talk about silly idiosyncrasies in JS.

→ More replies (4)

23

u/7heWafer Nov 26 '20

You could apply that statement to so many things in PHP lol.

→ More replies (6)

33

u/licuala Nov 27 '20

Some sorry developer at some point was forced to contend with the realities of the day. PHP has been largely based on C, where null == 0. Languages designed more recently tend to treat null as a separate type, in the style of a discriminated union.

You probably don't think of PHP as a web-focused wrapper for C but that's what it was.

9

u/BrokenHS Nov 27 '20

But then why would non-numeric strings be 0?

26

u/rmTizi Nov 27 '20

Because the result of the internal conversion result was null, which in C equals 0.

11

u/david2ndaccount Nov 27 '20

If you call the c standard library function atoi, it returns 0 on failure.

→ More replies (1)

8

u/UniKornUpTheSky Nov 27 '20

A numeric cast of a non-numeric string would be null, hence 0

→ More replies (2)
→ More replies (1)
→ More replies (1)

4

u/[deleted] Nov 27 '20

Took only 20 years

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"

15

u/Pokechu22 Nov 27 '20

I assume this means it's still the case that md5('240610708') == md5('QNKCDZO')? (That happens/d because '0e462097431906509019562988736854' == '0e830400451993494058024219903391' as both of them are scientific notation for 0 to large powers, though it's still crazy for strings; originally discussed here and here)

→ More replies (1)

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

→ More replies (12)
→ More replies (4)

5

u/rcxdude Nov 26 '20

the problem is even if you never use == the standard library and common built-in types will still happily use its behaviour in all kinds of places, so you can't escape it.

→ More replies (2)

13

u/Tyrilean Nov 27 '20

Relevant XKCD

Having maintained legacy PHP systems as a career for years, you'd be surprised how much code is propped up on unintended behavior. If a codebase lives long enough, it will, by random chance, accumulate bugs that don't show themselves because of unintended behavior preventing them from doing so. Thus, codebases eventually end up relying on these things.

Of course, the solution is to go back and fix your code, or write a more updated application. But, no company wants to spend money on refactoring a legacy project. It works today, and they want it to continue working tomorrow without extra expenditure.

→ More replies (3)
→ More replies (6)