尝试使用 PHP if/elseif 语句来节省时间

发布于 2024-12-05 19:16:33 字数 669 浏览 0 评论 0原文

我有一个相当大的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

维持三分热 2024-12-12 19:16:33

因为无论如何它们都是布尔值:

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
}

Since they are all bools anyway:

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
}
橘味果▽酱 2024-12-12 19:16:33

您可以将验证逻辑更改为类似的内容

$passed = array();
$failed = array();
if (!$result_spam)
{
   array_push($failed, "confirm_spam");
}
else
{
   array_push($passed, "confirm_spam");
}
...

然后您就有了一种简单明了的方法来检查是否所有通过/失败以及哪些测试失败。

You can change validation logic to something like

$passed = array();
$failed = array();
if (!$result_spam)
{
   array_push($failed, "confirm_spam");
}
else
{
   array_push($passed, "confirm_spam");
}
...

Then you have an easy and clear way to check whether all passed/failed and which tests are failed.

喜你已久 2024-12-12 19:16:33

如果你尝试这种方式怎么办:

$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 为空,则没有通过 else if $failed 不为空,最后一个还没有通过..你也是了解过去的事情和失败的事情,并利用它们做一些事情。您可以将结果存储在字符串或数组中,无论您想要什么......

What if you try this way:

$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 . ", ";
}

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文