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..
Asked
Active
Viewed 61 times
0

Álvaro González
- 142,137
- 41
- 261
- 360

Olli Lappalainen
- 21
- 2
-
4Because it is treated like 2 operators - `>` and `!`? – u_mulder Oct 25 '18 at 07:21
-
2I assume it’s not `>!`, it’s `>` and the `!` is “not” for the next argument. – Sami Kuhmonen Oct 25 '18 at 07:21
-
2To be explicit: `$foo >! $bar` === `$foo > (!$bar)` – Nick Oct 25 '18 at 07:25
-
Reminds me of [this](https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c) question from the C side of the site. – Andrei Oct 25 '18 at 07:33
-
1I bet you also didn't know the `$a++<++$a` operator and many others… ;) – deceze Oct 25 '18 at 07:33
2 Answers
4
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
!
to4
gives youfalse
- second, comparing
3
andfalse
gives youtrue
, because3
istruthy
value which is always greater than anyfalsy
/false
value.
As a practice you can understand this tricky cases:
var_dump(0 > !0); // false
var_dump(-3 > !0); // false

u_mulder
- 54,101
- 5
- 48
- 64
-
True. Now that reading this thread a while later this makes sense and should've been clear to me from the start... ::) thanks – Olli Lappalainen Feb 19 '19 at 07:19
0
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

Andrea Golin
- 3,259
- 1
- 17
- 23
-
-
Well, for *some* definition of "works"… It doesn't produce any syntax error, is probably what OP meant. – deceze Oct 25 '18 at 07:26
-
If `$b` != 0, `!$b == false` which is equivalent to `0` in a numeric comparison. So `10 > !5` and `10 > !11` – Nick Oct 25 '18 at 07:27
-
exactly it seemed to me that OP was considering it a strange "negation" of variable size comparison – Andrea Golin Oct 25 '18 at 07:27