Search code examples
phpcomparisonoperators

PHP >! operator isn't legit but works


So I used >! comparison operator in PHP 5.6 and it works. It doesn't appear on any operators documentation and I'm confused why does it work and why PHPStorm doesn't complain about it? Even if !($foo > $bar) would be the correct syntax..


Solution

  • Your >! operator is in fact two operators: > and !. ! is applied to second argument:

    var_dump(!4);     // `false`
    var_dump(3 >! 4); // `true`
    

    How come that last case it true:

    var_dump(3 >! 4) is same as var_dump(3 >(! 4)) because of operators precedence

    • first, applying ! to 4 gives you false
    • second, comparing 3 and false gives you true, because 3 is truthy value which is always greater than any falsy/false value.

    As a practice you can understand this tricky cases:

    var_dump(0 > !0);   // false
    var_dump(-3 > !0);  // false