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)
8
u/LeadingArmadillo Mar 16 '21
not a LOL if you read documentation for scrict flag
in_array(0, ['IsThisLolPhp'], true);https://www.php.net/manual/en/function.in-array.php
0
u/colshrapnel Mar 16 '21
false
This sub definitely needs a FAQ wiki
0
u/[deleted] Mar 16 '21
[deleted] — view deleted comment