Variable toggle in PHP?

Hello,

In most programming languages you can toggle variables like this:

$variabel = True;

$variabel = not $variabel;

then if variabel is true it becomes false, and next time true and so on?
Then I could use that to make swaping background color on Tables?

Can you somehow do this in PHP?

Yes:

if($variable == true)
$variable = false;

or

$variable = ($variable == true) ? false : true;

although if you are making the background color change so it is like
color1
color2
color1
color2
I would increment a integer, devide it by two and see if 1 or 0 remains, a good tutorial about this and the ternary operator exists here: http://www.sitepointforums.com/showthread.php?threadid=76466

If you are willing to use 0 and -1 instead of true and false:

$var = 0;
var_dump($var);
$var = ~$var;
var_dump($var);
$var = ~$var;
var_dump($var);
$var = ~$var;
var_dump($var);

produces

int(0)
int(-1)
int(0)
int(-1)

if($variable) {
$variable=false;
}

That would be shorter. You even have that in your user title :wink: .

still longer then my code because you need it to be:

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

opposed to

 $variable = ($variable == true) ? false : true; 

and you could I guess even drop the == true :wink:

I used the: $variable = ($variable == true) ? false : true; And it worked very well =)