PHP Double Not (!!) Operator

PHP | Double Not (!!) Operator: Here, we are going to learn about the Double Not (!!) Operator and its usages with example.
Submitted by Sayesha Singh, on August 31, 2020

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.

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.

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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.