简体   繁体   English

如何处理多个if语句PHP

[英]How to handle multiple if statements PHP

  • List item 项目清单

just wanted some opinions on the most efficient way to do this. 只是想对最有效的方法提出一些意见。 I have an email input and a phone input. 我有电子邮件输入和电话输入。 I have JQuery validation applied which works well. 我应用了JQuery验证,效果很好。 Basically, they both can't be empty. 基本上,它们都不能为空。 If only one is provided, that is fine. 如果只提供一个,那很好。 If both are provided, thats fine too. 如果两者都提供,那也可以。

Each field will call its own API. 每个字段将调用其自己的API。 If the phone field is provided, it will call a phone API, if the email field is provided it will call and email API, if both fields are provided, it will call both APIs. 如果提供了phone字段,它将调用一个电话API;如果提供了email字段,它将调用和email API;如果同时提供了两个字段,它将调用两个API。 To handle this, I pretty much do 为了解决这个问题,我几乎做了

if (isset($_POST["email"]) && !empty($_POST["email"])){
    //call email API
}
if (isset($_POST["phone"]) && !empty($_POST["phone"])){
    //call phone API
}

This seems to cover all the cases above, although there may be a more efficient way to do this. 尽管可能存在更有效的方法,但这似乎可以覆盖上述所有情况。

With my validation, I also want to record any time the submit button is pressed (it will fail validation and user wont know, I just want it recorded). 通过我的验证,我还想记录每当按下“提交”按钮时(它会通过验证而失败,用户将不知道,我只想记录它)。 So I have a invalidHandler set up and I grab the values of the inputs (if any) and pass them to PHP. 因此,我设置了invalidHandler,然后获取输入值(如果有)并将其传递给PHP。

What would be the best way to handle this in PHP though? 但是,用PHP处理此问题的最佳方法是什么? I dont want it firing if only one field is filled in because the above will handle that. 如果只填写一个字段,我不希望它触发,因为上面的内容可以解决这个问题。 I also dont want it firing if both fields are empty. 如果两个字段都为空,我也不希望它触发。 I only really want it firing if one or both fields have invalid data (failed validation). 我只希望它在一个或两个字段包含无效数据(验证失败)时触发。 If one field has valid data and another one does not, then I dont need to worry about this. 如果一个字段具有有效数据,而另一字段则没有,那么我就不必担心这一点。

So really just looking for the best way to handle something like this in PHP. 因此,实际上只是在寻找处理PHP中此类问题的最佳方法。

Thanks 谢谢

Not sure what you're really asking, but I think what you need is a flag for each check, and an OR opreator: 不确定您真正要问的是什么,但我认为您需要的是每张支票的标志和一个OR操作符:

$flag_phone = $flag_email = false;
if (phone_has_invalid_data()) {
    $flag_phone = true;
}
if (email_has_invalid_data()) {
    $flag_email = true;
}
if ($flag_email OR $flag_phone) {
    //throw some error
} else {
    //everything ok
}

(and speaking of unnecessary optimizations, the code if (isset($var) && !empty($var)) is equivalent to if (!empty($var)) ) (谈到不必要的优化,代码if (isset($var) && !empty($var))等同于if (!empty($var))

It's a reasonably pointless optimisation, but you can do this: 这是毫无意义的优化,但是您可以这样做:

if (isset($_POST["email"]) && !empty($_POST["email"])){
    //call email API
} elseif (isset($_POST["phone"]) && !empty($_POST["phone"])){
    //call phone API
}

Other than that, it looks fine 除此之外,看起来还不错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM