Double not (!!) operator in PHP

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// 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!";
else
    echo "Finish!";
?>

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.