Two exclamation points in php Laravel

double exclamation exclamation marks Laravel

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;

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store