PHP type juggling, “String” == 0 and “String” == true

前端 未结 2 1542
挽巷
挽巷 2020-12-07 04:14

I have a very simple question. In PHP,

if (\'abc\' == 0){
    //true
}
if (\'abc\' == 1){
    //false
}

I know that this page tell us that

2条回答
  •  爱一瞬间的悲伤
    2020-12-07 04:51

    The relevant table can be found here. It is important what type both operands have, based on that one or both values will be converted.

    In your first case, the rules of both sides being "string, resource or number" is the first applicable, and the rule is "Translate strings and resources to numbers, usual math".

    For the second example, the "Convert both sides to bool, FALSE < TRUE" rule fits.

提交回复
热议问题