# 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.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions