Boolean value switch/invert

PhpBoolean

Php Problem Overview


Is there a function for switching/inverting boolean value in PHP?

Like... a shortcut for:

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

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

What about using the Absolute Value function abs() , $val can be "1" or "0" and you want to invert it:

$val = abs($val-=1);

The logic:

Always subtracting "1" from the number and eliminating the "sign".

1 - 1 = 0
abs(0) = 0

0 - 1 = -1
abs(-1) = 1

Solution 3 - Php

If you want the shortest possible code, XOR the boolean with 1:

$boolean ^= 1;

Strictly this returns an int not a boolean. It doesn't work the same way as $boolean = !$boolean (and is slightly less efficient) but for most purposes it should do the job.

Solution 4 - Php

Just use ! to invert the result so it can be like: $boolean = !(bool)$result;

Solution 5 - Php

I compared all the ways listed, even the funny ones, to check what is faster, an included one more $bool = $bool!=true;:

$max=1000000;
$bool=true;

for($t=-microtime(true),$i=0;$i<$max;++$i){
	if($bool) $bool=false; else $bool=true;
}//Time 0.3589
echo sprintf("Time %.4F\n",$t+microtime(true));

for($t=-microtime(true),$i=0;$i<$max;++$i){
	switch($bool){ case true: $bool=false; break; default: $bool=true;}
}//Time 0.4387
echo sprintf("Time %.4F\n",$t+microtime(true));

for($t=-microtime(true),$i=0;$i<$max;++$i){
	$bool = !$bool;
}//Time 0.2833
echo sprintf("Time %.4F\n",$t+microtime(true));

for($t=-microtime(true),$i=0;$i<$max;++$i){
	$bool = $bool!=true;
}//Time 0.2803
echo sprintf("Time %.4F\n",$t+microtime(true));Time 0.2803

for($t=-microtime(true),$i=0;$i<$max;++$i){
	$bool = ($bool-1)*(-1);
}//Time 0.3047
echo sprintf("Time %.4F\n",$t+microtime(true));

for($t=-microtime(true),$i=0;$i<$max;++$i){
	$bool = abs($bool-1);
}//Time 1.2211
echo sprintf("Time %.4F\n",$t+microtime(true));

for($t=-microtime(true),$i=0;$i<$max;++$i){
	$bool ^= 1; //XOR
}//Time 0.2595
echo sprintf("Time %.4F\n",$t+microtime(true));

for($t=-microtime(true),$i=0;$i<$max;++$i){
	$bool = $bool?0:1;
}//Time 0.3253
echo sprintf("Time %.4F\n",$t+microtime(true));

Curiously, the obvious seems to be the 3rd in performance. XOR is a bit faster than logical complement (negation), and this last seems to be almost similar to the not equal operator that I added to the party.

Time to swap values 1 millon times:

  1. $bool ^= 1 -> 0.2595 seconds
  2. $bool = $bool!=true -> 0.2803 seconds
  3. $bool = !$bool -> 0.2833 seconds
  4. $bool = ($bool-1)*(-1) -> 0.3047 seconds
  5. $bool = $bool?0:1 -> 0.3253 seconds
  6. if -> 0.3589 seconds
  7. switch -> 0.4387 seconds
  8. $bool = abs($bool-1) -> 1.2211 seconds

Solution 6 - Php

One touch pick of boolean:

$detector = !$picker = $detector;

Solution 7 - Php

you can do it in one line :

<?php
$val = 0 
$val = $val ==1?0:1;  
?>

Solution 8 - Php

bool can be either TRUE or FALSE.

usage : (boolean)$red = varbool(false);
echo $red;

for true it will return zero and one for false

function varbool($val){	

	$val +=(-1);
    $val *= (-1);	
    return $val;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontomsseisumsView Question on Stackoverflow
Solution 1 - PhpPekkaView Answer on Stackoverflow
Solution 2 - PhpWagner LipnharskiView Answer on Stackoverflow
Solution 3 - PhpGannetView Answer on Stackoverflow
Solution 4 - PhpAbhinav bhardwajView Answer on Stackoverflow
Solution 5 - PhpLeopoldo SanczykView Answer on Stackoverflow
Solution 6 - PhplazycommitView Answer on Stackoverflow
Solution 7 - PhpsamView Answer on Stackoverflow
Solution 8 - Phpuser563836View Answer on Stackoverflow