php php检查2个变量是否等于另一个并且都等于0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21500703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php check 2 variables if they are equal one to another and both equal to 0
提问by john
as the tile says, i have 2 variables that must be equal one to another and both equal to 0
正如瓷砖所说,我有 2 个变量,它们必须彼此相等,并且都等于 0
i tried a few things but none worked:
我尝试了几件事,但没有奏效:
if (($getstatuschk === $getstatuspost) && ($getstatuschk === "0") && ($getstatuspost === "0")){ echo "status is ok";}
else {echo "not ok!"}
if (($getstatuschk === $getstatuspost) and ($getstatuschk === "0") and ($getstatuspost === "0")){ echo "status is ok";}
else {echo "not ok!"}
i hope you can help me whit this...
我希望你能帮我这个...
thanks!
谢谢!
回答by s3nzoM
Code should be
代码应该是
if (($getstatuschk == $getstatuspost) && ($getstatuspost == 0)){
echo "status is ok";
}
else {echo "not ok!"}
You don't need to check if both variables are equal to zero since you already compared them and don't put zero in quotes since this makes it a string
您不需要检查两个变量是否都为零,因为您已经比较了它们并且不要将零放在引号中,因为这使它成为一个字符串
回答by rrr
If they must be equal to each other and both equal to zero then apply basic boole algebra:
如果它们必须彼此相等并且都为零,则应用基本布尔代数:
if ($getstatuschk === 0 and $getstatuspost === 0){ echo "status is ok";}
else {echo "not ok!"}
Remember that a triple equal (===) also checks for data type not just value, if the two variables are known to be an integer use ===, if you don't know its type just use == and then cast it to integer using (== 0), remind that empty strings are also casted to 0's when comparing with integer.
请记住,三重等号 (===) 还检查数据类型,而不仅仅是值,如果已知两个变量是整数,请使用 ===,如果您不知道其类型,请使用 == 然后将其强制转换to integer using (== 0),提醒空字符串在与 integer 比较时也会被强制转换为 0。
回答by someoneHuman
The problem is that you are using quotes around your numbers. If you are using === then the things that you are comparing must be of the same type. So something like
问题是您在数字周围使用引号。如果您使用 ===,那么您要比较的东西必须是相同的类型。所以像
if (($getstatuschk === $getstatuspost) && ($getstatuschk === 0) && ($getstatuspost === 0)){ echo "status is ok";}
else {echo "not ok!"}
might be better.
可能会更好。

