How can I toggle a boolean number?

雨燕双飞 提交于 2019-12-10 12:27:45

问题


Here is my code:

$num = 1; // it always is either 0 or 1

if ( $num == 1 ){
    $num = 0
} else {
    $num = 1;
}

As you see, my code toggles the number. It is really ugly to me. I guessed php should has a function to do so. But I didn't find anything similar after some searches. Anyway, can I do that more standard and better?

Currectly I'm coding for a great company. That's why I want to write clean and professional codes.


回答1:


Your approach is correct and will work as well. Just you need to wrap it into a function. Another way is using ^ (Bitwise XOR) to do that functional and more clean:

function toggleNumber( $num ) {
    return $num ^= 1;
}

Online Demo


That function gets the number and does XOR with 1 on it. Then the number will be toggled.




回答2:


All Mothods below only use 1 line of code

Method 1) use ! operator

<?php 

$num = 1;

$num = (int) !$num;   // method 1

Method 2) use array to return with num as index

<?php 

  $num =1;

  $num = array(1,0)[$num];  //method 2

Method 3) use conditional operator

<?php 

  $num =1;

  $num = (int) ($num < 1);  // if 1 return 0 else return 1 , method 3

Method 4) Hack using empty function

<?php 

  $num = 1;

  $num = (int)empty($num);  // method 4

Oneline Demo




回答3:


You can simply use ! operator to toggle number between 0 and 1;

$num = 1; // it always is either 0 or 1
function toggleNumber($num)
{
  return !$num;
}
$num = intval(toggleNumber($num));
echo $num; //print 0
echo "<br>";
$num = intval(toggleNumber($num));
echo $num; // print 1

You can directly use $num = intval(!($num)); to toggle number. But function is good approach.




回答4:


Here's a couple of fun ways, if number is 1 return 0 or 1

$num = 1;
function toggle1($num){
   if($num){
      return 0;
   }
   return 1;
}

without argument, just toggle away for flip flop action

function toggle2(){
    static $num = 1;
    $num = ($num?0:1);
    return $num;
}

with argument

function toggle3($num){
    return ($num?0:1);
}

like @B. Desai but cast to int

function toggle4($num){
    return (int) !$num;
}

straight up like the example but ternary

$num = 1;
$num = ($num?0:1);



回答5:


Simple subtraction. Just subtract the current value from 1. This will not perform as quickly as bitwise, but it will be easily comprehensible by other coders if you are working in a team. The performance differences will be measurable in nanoseconds. $num=1-$num is a single line expression that doesn't use a conditional statement.

$num=1;

echo $num=1-$num,"\n";  // 0
echo $num=1-$num,"\n";  // 1
echo $num=1-$num;       // 0



回答6:


If you're coding for a great company, you don't want to use a number that will act as a boolean.

If you know for sure that your number will always be 0 or 1, then use a Boolean. Here are some reason:

  • In a few days/monthes/years, you will have to run through this code again, and you will see a number, so maybe you'll forget about that 0/1 rule that you implicitly set.

  • If somebody else read through your code, how is he going to know that this can only be 0/1 ?

  • When you (or someone else) will write documentation about that, do you really want to put in: "This is a number that can be only 0 or 1" ?

If you happend to use a boolean, that toggle thing you ask is much more easy:

$value = TRUE; //TRUE is a PHP Constant

$value = !$value; //Toggled, $value is now equal to FALSE

if ($value) { } //instead of if ($num == 1)
if (!$value) { } //instead of if ($num == 0)

In the documentation, you can state that this is a boolean value, everybody understands there are only two possibilities: TRUE or FALSE.




回答7:


In one line you can do it

$num = 1;
echo ($num == 1) ? 0 : 1;



回答8:


Another way to do it would be:

$num = 1 - $num;


来源:https://stackoverflow.com/questions/44608145/how-can-i-toggle-a-boolean-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!