PHP intval() weird results

前端 未结 5 1154
无人及你
无人及你 2020-12-20 06:26

I\'m encountering something weird and I don\'t know why it is happening!

I have a URL like:

http://mysite.com/users/USER_ID

this user id

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 07:03

    From http://php.net/manual/en/language.operators.comparison.php

    Example       Name        Result
    
    $a == $b      Equal       TRUE if $a is equal to $b after type juggling.
    
    $a === $b     Identical   TRUE if $a is equal to $b,
                              and they are of the same type. 
    

    Your string is casted to integer because of type juggling on == operator and intval() of a string returns 0

    This explains why $id == $id_inted in your code evaluates to true.

    If you make your test with === instead of == no type juggling will be performed.

提交回复
热议问题