why does this evaluate to true

后端 未结 2 1104
暖寄归人
暖寄归人 2021-01-24 18:48

Whey does this evaluate to true?

\';
}else{         


        
相关标签:
2条回答
  • 2021-01-24 19:29

    Give this a try: $val2==='error123'

    That will evaluate the value and the type of the variable. More here:

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

    0 讨论(0)
  • 2021-01-24 19:36

    You're comparing a string to an integer. To make the comparison the string is first converted to an integer. When 'error123' is converted to an integer it becomes 0.

    echo intval("error123");
    

    Result:

    0
    

    In the PHP manual there is an explanation for this behaviour.

    If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

    There is a quick reference page PHP type comparison tables that shows you the result of various comparions. See the table "Loose comparisons with ==". The interesting part with regard to this question is that 0 == "php" is shown as TRUE.

    There is also a page on type juggling. A user comment on that page gives nearly the exact same example as this.

    If you don't want the type juggling use === instead of ==.

    0 讨论(0)
提交回复
热议问题