Friday, July 31, 2009

How does one add a variable and a number in PHP?

I'm creating a verification script in PHP, to check if $var is set to "1". I figured using addition would be the easiest way to do so, so I tried this...





if ($var + 1) = 2){


print "The variable is set to 1.";


exit;


}





I must have an error in syntax or something, because I get an "unexpected '='" error when exeuting the script.





How do I fix up that code?

How does one add a variable and a number in PHP?
You need to use == which is the equal to comparison operator. A single = is an assignment.





$A=1 // Sets a to 1


$A==1 // Is $A equal to 1?The answer will be true or false.





More specifically:





if ($var + 1) == 2){


print "The variable is set to 1.";


exit;


}


No comments:

Post a Comment