PHP Double Not (!!) Operator

By Sayesha Singh Last updated : December 11, 2023

PHP Double Not (!!) / Not Not Operator

double not(!!): In PHP, the truthy value of a variable or expression is returned by double not(!!) or 'NOT NOT' Operator.

In layman's terms, it is a double negation/double complement of a variable or expression.

Complementing a variable/expression (by using the logical operator ! ) gives it negation. And then complementing it again results in the original boolean value that was present initially.

Further, it can be elaborated by saying the expression with a TRUE value when passed to a double not-operator don't change and stay TRUE. And a FALSE value of the expression also stays FALSE.

But if the value remains the same, then why is this operator used? It can be implemented to check that there was Boolean data (TRUE/FALSE) stored in an expression/variable.

Example of PHP Double Not (!!) Operator

<?php
// PHP program to demonstrate the example of
// Double NOT (!!) operator

// Declaring a variable
$value = 99;

// Checking conditions
if ($value !== 99) {
    echo "True 1: Checked with !==";
} elseif (!!$value) {
    echo "True 2: Check with Double NOT operator";
} else {
    echo "False.";
}
?>

The output of the above example is:

True 2: Check with Double NOT operator

PHP Not (!) Vs. Double Not (!!) Operator

The difference between the NOT (!) and the NOT NOT (!!) operator can be understood by the example that if an expression/variable is TRUE, then the former returns FALSE while the latter returns TRUE.

The following is a program that illustrates the effect of ! and !! operator on a Boolean expression.

Example

Program to illustrate the working of not operator(!) and double not operator (!!).

<?php 
    echo("Using ! operator for Boolean expression A.B \n");
    echo("A" . " ". "B" . " " . "EXP" . "\n");
    
    for ($i = 0; $i <= 1; $i++)
    {
        for ($j = 0; $j <= 1; $j++)
        {
           $expression = $i & $j;
           $expression = !$expression;
           if($expression==FALSE)
           $expression=0;
           echo($i . " ". $j . " " . $expression . "\n");
        }
    }
    
    echo("Using !! operator for Boolean expression A.B \n");
    echo("A" . " ". "B" . " " . "EXP" . "\n");
    
    for ($i = 0; $i <= 1; $i++)
    {
        for ($j = 0; $j <= 1; $j++)
        {
           $expression = $i & $j;
           $expression = !!$expression;
           if($expression==FALSE)
           $expression=0;
           echo($i . " ". $j . " " . $expression . "\n");
        }
    }
?>

Output:

Using ! operator for Boolean expression A.B

A B EXP
0 0 1
0 1 1
1 0 1
1 1 0
Using !! operator for Boolean expression A.B
A B EXP
0 0 0
0 1 0
1 0 0
1 1 1

Another example to understand the concept better. Now if there is any number, either integer or floating type, and if it is greater than 0, then it is treated as 1.

And hence when negated it gives the result 0. But when it is double negated through double not !! operator, it doesn't return the original value but 1.

It can be further understood with an example. Let us take the number 77. It will be treated as a TRUE boolean value and hence when complemented with ! gives us 0, that is a FALSE boolean value. Now complementing 0 gives us 1.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.