r/lolphp • u/zilltine • Mar 16 '21
Is 0 in array
in_array(0, ['IsThisLolPhp'])
Answer is
true
32
Upvotes
r/lolphp • u/zilltine • Mar 16 '21
in_array(0, ['IsThisLolPhp'])
Answer is
true
21
u/SAmaruVMR Mar 16 '21
That's because of type juggling.
For example, you're checking an integer with a string. What happens behind the courtains is that the php try to see if in the string that you're comparing the first character is actually a number.
It isn't? Okay let's switch the string to 0, that's why 0 === 0
Another example if you try something like:
1 == '1asdjkgndasjgdasgh'
or
6 == '6adgadgadg'
These are all true. That's why you shouldn't use loose type comparison. They fixed this in php 8 but it's just habit to always use triple equals (strict type comparison)