if statement - PHP - if == not working for 0 or "" -


this question has answer here:

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

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -