I’m just trying to write a function that can switch background color.
Each time it’s run, it should return the other color, but it’s not working
function bgcolour_switch(){
if(!isset($p)){
global $p;
$p = "#C0C0C0";
return $p;
}else{
if($p == "#C0C0C0"){
$p = "#FFFFFF";
return $p;
}elseif($p == "#FFFFFF"){
$p = "#C0C0C0";
return $p;
}
}
}
I keep getting the same color returned (#C0C0C0)
Well,
$pis never initially set in the scope of your function, so the if statement always evaluates to true.Try moving the
global $p;line to the beginning of the function, before the if statement.