尝试使用 PHP if/elseif 语句来节省时间
我有一个相当大的 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"
但是检查是否有些已通过而有些未通过,然后找到失败的会花费太长时间,对吗?
我只是想知道,是否有更简单/更快的方法来做到这一点?
I have a rather big if statement:
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";
}
Now I want to do another if statement to check if all have PASSED or if all have FAILED or is some have PASSED and some have FAILED and then echo (do something with) the failed ones.
I know how to check if all have passed or failed:
if ($confirm_spam == "PASSED" AND $confirm_analytics == "PASSED"
but to check if some have passed and some haven't and then find the ones that failed will take too long, right?
I was just wondering, would there be an easier/quicker way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为无论如何它们都是布尔值:
Since they are all bools anyway:
您可以将验证逻辑更改为类似的内容
然后您就有了一种简单明了的方法来检查是否所有通过/失败以及哪些测试失败。
You can change validation logic to something like
Then you have an easy and clear way to check whether all passed/failed and which tests are failed.
如果你尝试这种方式怎么办:
然后如果 var
$passed
为空,则没有通过 else if$failed
不为空,最后一个还没有通过..你也是了解过去的事情和失败的事情,并利用它们做一些事情。您可以将结果存储在字符串或数组中,无论您想要什么......What if you try this way:
Then if var
$passed
is empty, none passed else if$failed
is not empty, at last one have not passed.. so do you got what passed and what failed and do something with them. And you can store results both in a string or an array whatever you want...