Double not (!!) operator in PHP

In PHP, the double not operator (!!) is used to convert any value to its boolean equivalent. The first exclamation mark (!) negates the value, converting it to boolean and flipping its truthiness. The second exclamation mark (!) negates it again, effectively converting the original value to a clean boolean true or false.

How It Works

The double not operator works in two steps −

  • First ! converts the value to boolean and negates it
  • Second ! negates the result again, giving the original boolean value

Example 1: String to Boolean

Here's how to convert a string value to boolean using the double not operator −

<?php
    $str = "0.1";
    echo "Value = $str";
    $res = !!$str;
    echo "\nDouble Negated Value = $res";
?>
Value = 0.1
Double Negated Value = 1

Example 2: Number to Boolean

Let's see how it works with numeric values −

<?php
    $str = "100.56";
    echo "String = $str";
    $num = floatval($str);
    echo "\nNumber (Converted from String) = $num";
    $res = !!$num;
    echo "\nDouble Negated Value = $res";
?>
String = 100.56
Number (Converted from String) = 100.56
Double Negated Value = 1

Example 3: Different Value Types

Here's how various PHP values behave with the double not operator −

<?php
    $values = [0, "", "hello", null, [], [1,2,3], false, true];
    
    foreach($values as $value) {
        $boolean = !!$value;
        $type = gettype($value);
        echo "$type: " . var_export($value, true) . " -> " . ($boolean ? 'true' : 'false') . "<br>";
    }
?>
integer: 0 -> false
string: '' -> false
string: 'hello' -> true
NULL: NULL -> false
array: array (
) -> false
array: array (
  0 => 1,
  1 => 2,
  2 => 3,
) -> true
boolean: false -> false
boolean: true -> true

Conclusion

The double not operator (!!) is a quick way to convert any PHP value to its boolean equivalent. It's particularly useful for type conversion and ensuring you get a clean boolean result from mixed data types.

Updated on: 2026-03-15T08:20:17+05:30

556 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements