Open In App

Double not (!!) operator in PHP

Last Updated : 05 Dec, 2018
Like Article

The “NOT NOT” operator or Double not(!!) operator in PHP simply returns the truth value of the variable or expression. To explain in very simple terms, the first not operator(!) negates the expression. The second not operator(!) again negates the expression resulting the true value which was present before.

The (!!) operator returns as that Boolean function. If use !! to an expression the true value will be true and false value would be false. That is no change in the Boolean value. By using this double not(!!) operator it can increase the code readability and also ensure the truth and false values to be strictly Boolean data types.

Example 1:

// Declare a variable 
// and initialize it
$a = 1;
// Use double not operator
$a = !!$a;
// Display the value 
// of variable a.
echo $a;

Difference between logical NOT(!) operator and Double NOT(!!) operator in PHP: The Not operator is the mathematically complements or negates the Boolean value of the concerned data. For example a Boolean value of $a = True, then the NOT operator imposed on it !$a would be False. This is about logical NOT or Negation operator. Where as the Double NOT (!!) operator returns only the Boolean cast or the truth value. That is !!$a is always TRUE.
Here is an another example based on double NOT operator.

Example 2:

// PHP program to illustrate
// Double NOT operator
// Declare a variable and 
// initialize it
$t = 10;
// Check condition
if ($t !== 10)
    echo "This is NOT operator!";
elseif (!!$t)
    echo "This is Double NOT operator!";
    echo "Finish!";

The above code strictly save the Boolean data type and returns the truth value of the variable.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads