[英]php “if” condition mystery
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?为什么if条件通过? Why does php echo "blah"?为什么 php 回显“blah”? What do I do to make php evaluate the "if" statement and print "foo"?我该怎么做才能让 php 评估“if”语句并打印“foo”?
The problem here is that you're putting your expressions in strings!这里的问题是您将表达式放在字符串中!
Your $condition1
, $condition2
, and $condition3
variables contain strings, and not the result of an expression, and the same goes for your $condition
variable which will be a string that looks like 53==56||53==57||53==58
.您的$condition1
、 $condition2
和$condition3
变量包含字符串,而不是表达式的结果,您的$condition
变量也是如此,它将是一个看起来像53==56||53==57||53==58
的字符串53==56||53==57||53==58
。 When PHP evaluates a string it considers it true
if it is not empty and not equal to 0
, so your script will output blah
.当 PHP 评估一个字符串时,如果它不为空且不等于0
,则认为它为true
,因此您的脚本将为 output blah
。
To fix this you just need to take your expressions out of the strings.要解决此问题,您只需将表达式从字符串中取出。 It should look like this:它应该如下所示:
$condition1 = 53 == 56; // false
$condition2 = 53 == 57; // false
$condition3 = 53 == 58; // false
$condition = $condition1 || $condition2 || $condition3; // false || false || false = false
if ($condition) {
echo 'blah';
} else {
echo 'foo'; // This will be output
}
You're evaluating strings as booleans;您正在将字符串评估为布尔值; they'll aways be true (except the strings ""
and "0"
. Get rid of almost all of the quotes in your program.它们将是真的(字符串""
和"0"
除外。去掉程序中几乎所有的引号。
Those aren't conditions, they're strings.这些不是条件,它们是字符串。
$condition1=53==56;
$condition2=53==57;
$condition3=53==58;
$condition=$condition1 || $condition2 || $condition3;
if($condition)
{
echo "blah";
}
else
{
echo "foo";
}
Because you're not checking those variables, it's saying if (String)
will always return true.因为您没有检查这些变量,所以它说if (String)
将始终返回 true。 (unless ""
) (除非""
)
You should be doing:你应该这样做:
if(53==56 || 53==57 || 53==58)
{
echo "blah";
}
else
{
echo "foo";
}
All $condition*
variables will evaluate to true.所有$condition*
变量都将评估为真。 This is how PHP sees it:这就是 PHP 的看法:
if("53==56" || "53==57" || "53==58")
What you want is this:你想要的是这样的:
$condition1 = 53==56;
$condition2 = 53==57;
$condition3 = 53==58;
It's because you're evaluating a string, and strings other than empty strings evaluate to true.这是因为您正在评估一个字符串,而空字符串以外的字符串评估为真。
You are concatting a string together, a non-empty string equals TRUE
in php.您正在将一个字符串连接在一起,一个非空字符串在 php 中等于TRUE
。
Because when the if passes, $condition is a string (a concatenation of) containing the text of your conditions.因为当 if 通过时, $condition 是一个包含条件文本的字符串(的串联)。 Try using if(eval($condition))
.尝试使用if(eval($condition))
。
String always evaluate to true if its not empty如果字符串不为空,则字符串始终评估为真
And btw php make implicit conversion to boolean顺便说一句 php 隐式转换为 boolean
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.