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;
}
63703 次浏览

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

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;
}

One touch pick of boolean:

$detector = !$picker = $detector;

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

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.

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

you can do it in one line :

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

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