I am running into a funny problem with a mischievous “if” condition :
$condition1="53==56";
$condition2="53==57";
$condition3="53==58";
$condition=$condition1."||".$condition2."||".$condition3;
if($condition)
{
echo "blah";
}
else
{
echo "foo";
}
Why does the if condition pass?
Why does php echo “blah”? What do I do to make php evaluate the “if” statement and print “foo”?
The problem here is that you’re putting your expressions in strings!
Your
$condition1,$condition2, and$condition3variables contain strings, and not the result of an expression, and the same goes for your$conditionvariable which will be a string that looks like53==56||53==57||53==58. When PHP evaluates a string it considers ittrueif it is not empty and not equal to0, so your script will outputblah.To fix this you just need to take your expressions out of the strings. It should look like this: