Olli Lappalainen
Olli Lappalainen

Reputation: 21

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..

Upvotes: 0

Views: 75

Answers (2)

u_mulder
u_mulder

Reputation: 54841

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

Upvotes: 4

Andrea Golin
Andrea Golin

Reputation: 3559

It does not seems to work for me, as a variable comparison operator. In php 5.6, results are inconsistent:

$a = 10;
$b = 5;

var_dump($a >! $b);

returns true

but

$a = 10;
$b = 11;

var_dump($a >! $b);

returns true again

As others have stated, your variable is being evaluated as false, which make the if statement to returns true in the code above

Upvotes: 0

Related Questions