php - Unexpected behaviour of Intval -
this question has answer here:
- php intval() weird results 5 answers
here, $username
userinput, , trying check if entry username, or userid (all integers)
i thought use intval function see if $username
, intval($username)
same, means input userid.
the input gave google
. , intval('google')
0. why true part of if statement executed? idea?
i amnt using ===
because userinput string.
if($username == intval($username)) { echo "userid"; } else { echo "username"; }
not sure why unexpected behaviour happening.
it happening because of conversion
& type juggling
of comparison operators.
intval('anystring')
0
.
and when string
getting compared converted numeric value. when string converted 0
.
if compare number string or comparison involves numerical strings, each string converted number , comparison performed numerically. these rules apply switch statement. type conversion not take place when comparison === or !== involves comparing type value.
so in case 'google1' == intval('google')
0 == 0
, true. type of comparison use identical(===) comparison.
Comments
Post a Comment