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.
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;
}
That function gets the number and does XOR with 1
on it. Then the number will be toggled.
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.
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);
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,"
"; // 0
echo $num=1-$num,"
"; // 1
echo $num=1-$num; // 0
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.
In one line you can do it
$num = 1; echo ($num == 1) ? 0 : 1;
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
Another way to do it would be:
$num = 1 - $num;