4 ms·
0 == 'foobar' // false > When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string co
by CraneWorm 6y ago
0 == 'foobar' // false
> When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison.
Why do the conversion at all? Why couldn't this just be an error? I realise there is also `===` the strict comparison that typechecks arguments, but why not make that the default?
- baby 6y ago> Why couldn't this just be an error? you mean crash your program? So your user finds a way to send you an int instead of a string, or vice versa, and now they can crash your webserver.
- mgkimsal 6y agotechnically they'd just crash that one php process, not the whole web server. :) I think the bigger 'why not' is ... 25 years of backwards compatibility.
- baby 6y agoOh right ^^ that’s actually one really cool thing about php web apps, no easy denial of service attacks
- mgkimsal 6y ago(again) technically it could trigger a warning (actually, I thought perhaps it did that years ago??) It's still pretty easy to DOS the server that is passing requests to PHP. The 'shared nothing' approach has other benefits though - primarily not having to concern yourself with concurrency issues or shared memory resources. You can dive in to deep PHP and do that, but it's not something beginners need to be thinking about.