Type casting and Comparison with Loose Operator “==”

前端 未结 7 2281
暖寄归人
暖寄归人 2020-12-10 16:58

I have a problem baffling me terribly. I noticed this before but didn\'t give it any heed until today. I was trying to write my own check for integer strings. I know of

7条回答
  •  独厮守ぢ
    2020-12-10 17:21

    First of all in mathematices '=' is called transitive b/c (A=B and B=C => A=C) is valid.

    This is not the case with PHPs "=="!

    (int)$var1 == $var1
    

    In that case PHP will cast 'string' to 0 - that's a convention.

    Then ==-operator will implicitely have the second operand 'string' also be casted to integer -> as well 0.

    That leads to true.

提交回复
热议问题