Two exclamation points in php Laravel

double exclamation exclamation marks Laravel

Panjeh
1 min readJul 25, 2019

Double not-operator

return !! $value;

In fact it is casting to bool. People cast to bool by using !!

It is equal to :

return (bool) $value;

You can interpret it in this way:

It’s the not ! operator applied twice. The right ! one will result in a boolean and also affects as a not ( changes true to false or false to true), regardless of the operand. Then the left ! will negate that boolean. So it means we applied twice the ! operator.

This means that for any true value (numbers other than zero, non-empty strings and arrays, etc.) you will get the boolean value TRUE, and for any false value (0, 0.0, NULL, empty strings or empty arrays) you will get the boolean value FALSE.

It is functionally equivalent to a cast to boolean:

return (bool) $value;

Before you continue with the rest of this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

--

--

Panjeh
Panjeh

Written by Panjeh

Posting about Python and Laravel

Responses (1)