我有一个相当大的if语句:
if (!$result_spam)
{
$confrim_spam = "FAILED";
}
else if ($result_spam)
{
$confrim_spam = "PASSED";
}
if (!$result_email_manage)
{
$confrim_email_manage = "FAILED";
}
else if ($result_email_manage)
{
$confrim_email_manage = "PASSED";
}
if (!$result_analyt)
{
$confrim_analytics = "FAILED";
}
else if ($result_analyt)
{
$confrim_analytics = "PASSED";
}
现在我想做另一个if语句来检查是否所有人都已经失败,或者是否所有人都已经失败,或者有些人已经失败,而且有些人已经失败,然后回复(做某事)失败者。
我知道如何检查是否已经通过或失败:
if ($confirm_spam == "PASSED" AND $confirm_analytics == "PASSED"
但要检查一些是否已经通过,有些没有,然后找到失败的那些将花费太长时间,对吗?
我只是想知道,有没有更容易/更快的方法来做到这一点?
答案 0 :(得分:2)
因为无论如何他们都是bo ::
if($result_spam && $result_email_manage && $result_analyt){
//do all passed
}
elseif($result_spam || $result_email_manage || $result_analyt){
//at least one passed
if(!$result_spam){ echo '$result_spam failed';}
if(!$result_email_manage){ echo '$result_email_manage failed';}
if(!$result_analyt){ echo '$result_analyt failed';}
}
else {
//do all failed
}
答案 1 :(得分:1)
您可以将验证逻辑更改为
$passed = array();
$failed = array();
if (!$result_spam)
{
array_push($failed, "confirm_spam");
}
else
{
array_push($passed, "confirm_spam");
}
...
然后,您可以轻松清晰地检查是否所有通过/失败以及哪些测试失败。
答案 2 :(得分:1)
如果你这样尝试怎么办:
$passed = $failed = "";
$all = array("confrim_spam" => $result_spam,
"confrim_email_manage" => $result_email_manage,
"confrim_analytics" => $result_analyt);
foreach($all as $a => $b)
{
if (!$b)
$failed.= $a . ", ";
else
$passed.= $a . ", ";
}
然后如果var $passed
为空,如果$failed
不为空,则没有传递,否则最后一个没有通过..所以你得到了什么,失败了什么并用它们做了些什么。并且您可以将结果存储在字符串或数组中,无论您想要什么......