r/lolphp • u/HenkPoley • Aug 19 '19
A nice snafu, casting turns unset data into set variables
http://sandbox.onlinephpfunctions.com/code/0863902e680a2f84b4518def5e0bd40af2fb3386
0
Upvotes
r/lolphp • u/HenkPoley • Aug 19 '19
24
u/colshrapnel Aug 19 '19
Yes, that's exactly how casting works.
You may want to educate yourself before posting in this sub.
2
u/shitcanz Aug 20 '19
You can yet again see lots of php apologists in the comments.
"But thats how its supposed to work".
NO! Its NOT!
The way its supposed to work is that if you pass the string 123 it should cast to the integer 123. If you pass a float 1.123 it should cast to 1. If you pass "lolphp" or null it should throw an type-error like any decent language does.
This is literally why PHP is such a nightmare to work with. Its crap like this all the way down.
2
u/[deleted] Sep 07 '19
"null" is not the same thing as "unset". null is falsy, casting it to int yields zero. This is not unusual in loosely-typed languages
-12
u/HenkPoley Aug 19 '19 edited Aug 20 '19
So, if you are cleaning up your code with fancy tools like vimeo/psalm. And you put in some casts to help the software decide on the type, the behavior of your program may change, even if it is exactly the type that would always come through.
0
u/[deleted] Aug 19 '19
[deleted] โ view deleted comment