198
votes

What does the double not operator do in PHP?

For example:

return !! $row;

What would the code above do?

6
This is essentially the same as doing return (bool) $row;WildlyInaccurate
I was wondering which of them is faster and we have (bool) as winner. i.stack.imgur.com/dcnsM.pngBinod Kalathil
@Binod that's a nice-to-have info, thanks for that!Robin K

6 Answers

305
votes

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;
52
votes

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 ;)

18
votes

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

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

1
votes

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

0
votes

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.