if statement - PHP - if == not working for 0 or "" -
this question has answer here:
- why php consider 0 equal string? 6 answers
i checking validation of request if query,
if ($request_userid == $userid) { ... }
thats working expected. further testing has shown, if $request_userid
or $userid
0 or "", condition true , script runs if query shouldn't.
i solving with:
if ($userid == "" ) { exit ("exit"); }
but don't think right way?
can explain why doesn’t work if query , correct way check it?
here the php page on comparison operators.
you're using loose comparison using double-equals sign. change triple equals sign , check both type , value.
also see page on booleans & casting.
the reason '' == 0
evaluates true, integer 0, when cast boolean, converts false. empty string (''
) converts false. therefore, comparison ends looking if (false == false)
, true.
Comments
Post a Comment