How to Use the Double Not(!!) Operator in PHP

Maybe you have already met the double not (!!) or the NOT NOT operator while working in PHP.

Here, we will explain how it is used with the help of several examples.

Watch a course Learn object oriented PHP

Using the Double Not(!!) Operator

This PHP operator is aimed at returning the boolean value of an expression or a variable. To be more explicit: the first not operator (!) will negate the expression, the second one (!) will negate the first expression, bringing back the initial, boolean value.

The double not (!!) operator can be used for increasing the code readability and guaranteeing that the result is Boolean data type.

For a better understanding, check out the example below:

<?php

// Declaring a variable
// and initializing it
$a = 1;

// Using double not operator
$a = !!$a;

// Displaying the value
// of variable a.
echo $a;

?>

How the NOT(!) and Double NOT(!!) Operators Differ from Each Other

The Not (!) operator is aimed at negating the concerned data’s Boolean value. For instance, if the value of $a is true, then imposing the Not operator on it (!$a) will make it false. So, note that the double not(!!) operator will always be true.

Let’s see another example:

<?php

// PHP program to demonstrate
// Double NOT operator

// Declaring a variable and
// initializing it
$t = 10;

// Checking condition
if ($t !== 10) {
  echo "This is NOT operator!";
} elseif (!!$t) {
  echo "This is Double NOT operator!";
} else {
  echo "Finish!";
}

?>

So, the example above shows that the Boolean data type is saved, and the true value of the variable is returned.