1 > false) returns true?

This question already has an answer here:

  • Type-juggling and (strict) greater/lesser-than comparisons in PHP 3 answers

  • In this case it's -1 that is converted into boolean ( true , as only 0 is treated as false ). So the final comparison is

    if (true > false) {
        ...
    }
    

    Type Juggling can be very unintuitive, so always try to avoid situations where you compare variables of two different types. In case of equality comparison always try to use identity operator ( === ), in case of inequality all you can do is to add a manual cast.

    See also: http://us3.php.net/manual/en/types.comparisons.php


    Please see PHP Comparision Operators - table Comparison with Various Types

    Type of Operand 1   Type of Operand 2       Result
    bool or null        anything                Convert both sides to bool, FALSE < TRUE
    

    So if you compare bool to anything else, then the second operand is casted to boolean true. Also we have information here, that FALSE < TRUE , what exactly happens in your example.


    <是一个数字比较运算符,代码没有宽松比较,将-1转换为真,从而得到结果。

    链接地址: http://www.djcxy.com/p/58508.html

    上一篇: 在PHP中查找最大的三个值

    下一篇: 1> false)返回true?