John John - 4 months ago 30
PHP Question

PHP Flip Boolean Value

I have a boolean value that I would like to flip between true and false. The basic implementation of this is:

if ($my_boolean) {
$my_boolean = false;
}
else {
$my_boolean = true;
}


Is there a way to simplify this and reduce the number of lines of code?

Answer

You can use this:

$my_boolean = !$my_boolean;