213

What does the double not operator do in PHP?

For example:

return !! $row;

What would the code above do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrei
  • 4,289
  • 4
  • 25
  • 23

6 Answers6

328

It's not the "double not operator", it's the not operator applied twice. The right ! will result in a boolean, regardless of the operand. Then the left ! will negate that boolean.

This means that for any true value (numbers other than zero, non-empty strings and arrays, etc.) you will get the boolean value TRUE, and for any false value (0, 0.0, NULL, empty strings or empty arrays) you will get the boolean value FALSE.

It is functionally equivalent to a cast to boolean:

return (bool)$row;
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Theo
  • 131,503
  • 21
  • 160
  • 205
  • 26
    "double-not-operator" or "double not-operator" .. depends how you read it. – nickf Jan 24 '10 at 14:24
  • 11
    @nickf not really, "the double not operator" refers to one operator, not the act of using the not operator twice. – Theo Jan 26 '10 at 09:13
  • 4
    @Theo, It's actually one operator? Does the interpreter consider `!!` equal to `(bool)` in this case? Or will different machine code be run depending on which one is used? – Pacerier Mar 30 '15 at 12:00
  • This Works for JavaScript too, Here's a list of [Falsy Values For Javascript](http://www.sitepoint.com/javascript-truthy-falsy/) and heres a list of [Falsy Values For PHP](http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting) – Jomar Sevillejo Sep 08 '15 at 04:34
  • 2
    You can use as many as you want. If there is an odd number of 'bangs' (exclamation points) it will be a NOT operation same as `!value`, and if there is an even number of bangs it will be the same as `(bool) value`. – diamondsea Mar 22 '18 at 03:24
  • I've known this as the "bang-bang" operator. – Andre Ravazzi May 23 '20 at 05:50
56

It's the same (or almost the same - there might be some corner case) as casting to bool. If $row would cast to true, then !! $row is also true.

But if you want to achieve (bool) $row, you should probably use just that - and not some "interesting" expressions ;)

viraptor
  • 33,322
  • 10
  • 107
  • 191
  • 2
    Thank's that actualy makes sense, I nevers seen something like this in other programing language. – Andrei Jan 24 '10 at 16:58
  • 17
    Using `!!` is a habit remaining from programming in C(++). In C doing a cast isn't as easy as in PHP, you can get many different problems, compile warnings, a.s.o. Thus people cast to boll by using `!!`. – NikiC Sep 20 '10 at 15:03
  • 1
    @nikic - It sounds strange, IMO... since `operator!` is overloadable, using it in `C++` would be dangerous. Also you cannot cast structs in C, or use `!` with them. Also if something can be negated, you're most likely able to return it from a function returning int, which pretty much makes it a boolean. Are you sure you're talking about C? – viraptor Sep 20 '10 at 21:18
  • Hum, I'm no C programmer, admittedly. But I have seen some commits in software there a cast was replaced by `!!` and assumed that that's due to compiler warnings. Now I researched a little bit and found this: [http://stackoverflow.com/questions/206564/what-is-the-performance-implication-of-converting-to-bool-in-c](Performance Warnings in VS). – NikiC Sep 21 '10 at 13:30
  • 2
    I have seen !! in many C programs as a safe conversion to 1/0. (As boolean really is nothing but 0/"non zero" in C.) – Prof. Falken Feb 26 '13 at 14:55
  • 1
    @viraptor, `!!` is nothing "interesting" as you so claimed. It's all over the place from C to JavaScript. – Pacerier Mar 30 '15 at 12:01
  • 3
    @Pacerier hence the quotes. It's "interesting" as in - does the same thing as a cast, but uses potentially confusing syntax. – viraptor Apr 19 '15 at 08:06
  • 1
    @viraptor, I mean, it's a well-known idiom across many languages. So it's not exactly confusing in any way. – Pacerier May 24 '15 at 21:48
20

It means if $row has a truthy value, it will return true, otherwise false, converting to a boolean value.

Here is example expressions to boolean conversion from php docs.

Expression             Boolean
$x = "";               FALSE
$x = null;             FALSE
var $x;                FALSE
$x is undefined        FALSE
$x = array();          FALSE
$x = array('a', 'b');  TRUE
$x = false;            FALSE
$x = true;             TRUE
$x = 1;                TRUE
$x = 42;               TRUE
$x = 0;                FALSE
$x = -1;               TRUE
$x = "1";              TRUE
$x = "0";              FALSE
$x = "-1";             TRUE
$x = "php";            TRUE
$x = "true";           TRUE
$x = "false";          TRUE
YOU
  • 120,166
  • 34
  • 186
  • 219
7

"not not" is a convenient way in many languages for understanding what truth value the language assigns to the result of any expression. For example, in Python:

>>> not not []
False
>>> not not [False]
True

It can be convenient in places where you want to reduce a complex value down to something like "is there a value at all?".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • 1
    What a wretched example. Better to show >>> not not True. Well qualified to explain absence of a value at all, though. – mckenzm Mar 23 '22 at 03:52
2

Another more human, maybe simpler, way to 'read' the not not:

  • The first '!' does 2 things: 'convert' the value to boolean, then output its opposite. So it will give true if the value is a 'falsy' one.

  • The second '!' is just to output the opposite of the first.

So, basically, the input value can be anything, maybe a string, but you want a boolean output, so use the first '!'. At this point, if you want TRUE when the input value is 'falsy', then stop here and just use a single '!'; otherwise if you want TRUE when the input value is 'truthy', then add another '!'.

Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
-1

Lets look at

!$a;

Rather than interpreting the ! operator as as taking the

Boolean opposite of the value to its right

read

take the Boolean opposite of the expression to its right

In this case

$a;

could be an expression

so to is

!$a;

so is

!!$a;

and

!!!$a;

and so on, as each of these is a valid expression, the ! operator can be prepended to each of them.

Chris Rutledge
  • 357
  • 3
  • 6