Double not (!!) operator in PHP

PhpOperators

Php Problem Overview


What does the double not operator do in PHP?

For example:

return !! $row;

What would the code above do?

Php Solutions


Solution 1 - Php

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;

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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

Solution 6 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreiView Question on Stackoverflow
Solution 1 - PhpTheoView Answer on Stackoverflow
Solution 2 - PhpviraptorView Answer on Stackoverflow
Solution 3 - PhpYOUView Answer on Stackoverflow
Solution 4 - PhpSteve BennettView Answer on Stackoverflow
Solution 5 - PhpLuca ReghellinView Answer on Stackoverflow
Solution 6 - PhpChris RutledgeView Answer on Stackoverflow