Double Negations in PHP

2020-05-08

Today we are going to look at this weird thing called double negations in PHP and what they are used for. And just to be clear, I'm talking about using !! in PHP 😉

The other day I saw something in a video on laracasts.com (opens new window), that looked very weird to me. The weird thing in question was a double negation 👉 !! in a return statement.

In the first moment I was just wondering if this is a new kind of special operator that I didn't know about yet. I went to Google and started searching. Quickly I realized this is just a nice shorthand to transform any value into a boolean.

return !! $value; // will always return true or false

So how does this work? The first ! will transform the value into a boolean. This happens through negating the given value. The second ! is used to restore the values original state in terms of its boolean representation.

(null) // false
(! null) // true
(!! null) // false

Another nice example would be to use this in unit tests.

return !! $user->is_active;

// or

$this->assertTrue(!! $user->is_active);

And that's basically it. Now you have a nice method at hand to transform any value into a boolean.

Have fun playing around with it! 😁