jolt jolt - 6 months ago 37
PHP Question

Boolean value switch/invert

Is there a function for switching/inverting

boolean
value in PHP?

Like... a shortcut for:

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

Answer

Yes:

$boolean = !$boolean;

if it's not a boolean value, you can use the ternary construction:

$int = ($some_condition ? 1 : 2); // if $some_condition is true, set 1
                                  // otherwise set 2
Comments