简体   繁体   English

在PHP中,如何在if语句的if中执行if语句?

[英]In PHP how do you do an if statement within the if of an if statement?

So the question is confusing I know. 所以我知道问题很混乱。 This is what I am wondering how to do. 这就是我想知道该怎么做。

I have the following if statement: 我有以下if语句:

if(
    (isset($_POST['billing_company']) && $_POST['billing_company'] != "") && 
    (isset($_POST['billing_address']) && $_POST['billing_address'] != "") && 
    (isset($_POST['billing_city']) && $_POST['billing_city'] != "") && 
    (isset($_POST['billing_state']) && $_POST['billing_state'] != "") && 
    (isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") && 
    (isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") && 
    (isset($_POST['location_name']) && $_POST['location_name'] != "") && 
    (isset($_POST['location_address']) && $_POST['location_address'] != "") && 
    (isset($_POST['location_city']) && $_POST['location_city'] != "") && 
    (isset($_POST['location_state']) && $_POST['location_state'] != "") && 
    (isset($_POST['location_zip']) && $_POST['location_zip'] != "") && 
    (isset($_POST['location_phone']) && $_POST['location_phone'] != "") && 
    (isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") && 
    (isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") && 
    (isset($_POST['user_username']) && $_POST['user_username'] != "") && 
    (isset($_POST['user_email']) && $_POST['user_email'] != "") && 
    (isset($_POST['user_password']) && $_POST['user_password'] != "") && 
    (isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") && 
    (isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") && 
    (isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") && 
    (isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") && 
    (isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") && 
    (isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") && 
    (isset($_POST['terms']) && $_POST['terms'] != "")
){

However, based on some variables, I may no longer need any of the billing information. 但是,根据一些变量,我可能不再需要任何计费信息。 So I am wondering if I can do an IF inside the IF, so something like this: 所以我想知道我是否可以在IF中做一个IF,所以像这样:

if(
    ($billingRequired == 1){
        (isset($_POST['billing_company']) && $_POST['billing_company'] != "") && 
        (isset($_POST['billing_address']) && $_POST['billing_address'] != "") && 
        (isset($_POST['billing_city']) && $_POST['billing_city'] != "") && 
        (isset($_POST['billing_state']) && $_POST['billing_state'] != "") && 
        (isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") && 
        (isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") && 
    }
    (isset($_POST['location_name']) && $_POST['location_name'] != "") && 
    (isset($_POST['location_address']) && $_POST['location_address'] != "") && 
    (isset($_POST['location_city']) && $_POST['location_city'] != "") && 
    (isset($_POST['location_state']) && $_POST['location_state'] != "") && 
    (isset($_POST['location_zip']) && $_POST['location_zip'] != "") && 
    (isset($_POST['location_phone']) && $_POST['location_phone'] != "") && 
    (isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") && 
    (isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") && 
    (isset($_POST['user_username']) && $_POST['user_username'] != "") && 
    (isset($_POST['user_email']) && $_POST['user_email'] != "") && 
    (isset($_POST['user_password']) && $_POST['user_password'] != "") && 
    (isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") && 
    (isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") && 
    (isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") && 
    (isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") && 
    (isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") && 
    (isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") && 
    (isset($_POST['terms']) && $_POST['terms'] != "")
){

I'm pretty sure there isn't but wanted to check with people smarter than me. 我很确定没有,但想与比我更聪明的人检查。 I know I can do some nesting in side the {} but wanted to not have to check each variable inside a very deep nest. 我知道我可以在{}旁边做一些嵌套但是不想在非常深的嵌套中检查每个变量。

Thanks, 谢谢,

Just add () around the section you want to combine and it will resolve to a simple boolean which can be included in your if statement 只需在要组合的部分周围添加(),它就会解析为一个简单的布尔值,它可以包含在你的if语句中

The logic here reads "if (billing is not required or billing fields are all filled out) and all the non-billing fields are filled out, then..." 这里的逻辑显示“if(不需要计费或计费字段全部填写完毕)并填写所有非计费字段,然后......”

if(
    (($billingRequired != 1) || (
        (isset($_POST['billing_company']) && $_POST['billing_company'] != "") && 
        (isset($_POST['billing_address']) && $_POST['billing_address'] != "") && 
        (isset($_POST['billing_city']) && $_POST['billing_city'] != "") && 
        (isset($_POST['billing_state']) && $_POST['billing_state'] != "") && 
        (isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") && 
        (isset($_POST['billing_phone']) && $_POST['billing_phone'] != "")
    ))
    &&
    (
        (isset($_POST['location_name']) && $_POST['location_name'] != "") && 
        (isset($_POST['location_address']) && $_POST['location_address'] != "") && 
        (isset($_POST['location_city']) && $_POST['location_city'] != "") && 
        (isset($_POST['location_state']) && $_POST['location_state'] != "") && 
        (isset($_POST['location_zip']) && $_POST['location_zip'] != "") && 
        (isset($_POST['location_phone']) && $_POST['location_phone'] != "") && 
        (isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") && 
        (isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") && 
        (isset($_POST['user_username']) && $_POST['user_username'] != "") && 
        (isset($_POST['user_email']) && $_POST['user_email'] != "") && 
        (isset($_POST['user_password']) && $_POST['user_password'] != "") && 
        (isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") && 
        (isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") && 
        (isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") && 
        (isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") && 
        (isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") && 
        (isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") && 
        (isset($_POST['terms']) && $_POST['terms'] != "")
    )
{
    //statements
}

Although I suspect this could be done better within a loop, maybe it's personal preference, but I'd be happier with something like this 虽然我怀疑这可以在一个循环中做得更好,也许这是个人偏好,但我会更喜欢这样的事情

$billingRequiredFields = array('billing_company','billing_address','billing_city','billing_state','billing_zip','billing_phone','location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms');
$billingNotRequiredFields = array('location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms')
$requiredFields = ($billingRequired == 1) ? $billingRequiredFields : $billingNotRequiredFields;
$continue = true;
foreach($requiredFields as $field) {
    if (!isset($_POST[$field]) || $_POST[$field] == '') {
        $continue = false;
        break;
    }
}

if ($continue) {
    // statements
}

better yet, don't put so many tests in the primary conditional, just make a function to test it, and then test for === true or === false 更好的是,不要在主条件中放置这么多测试,只需要做一个函数来测试它,然后测试=== true或=== false

function validate_input($billingRequired=0){
    $b_valid = true;
    if( $billingRequired == 1 ){
         if (!isset($_POST['billing_company']) || $_POST['billing_company'] == ""){
            $b_valid = false;
         }
         elseif (!isset($_POST['billing_address']) || $_POST['billing_address'] == ""){
            $b_valid = false;
         }
         elseif (!isset($_POST['billing_city']) || $_POST['billing_city'] == ""){
            $b_valid = false;
         }
         elseif (!isset($_POST['billing_state']) || $_POST['billing_state'] == ""){
            $b_valid = false;
         }
         elseif (!isset($_POST['billing_zip']) || $_POST['billing_zip'] == ""){
            $b_valid = false;
         }
         elseif (!isset($_POST['billing_phone']) || $_POST['billing_phone'] == ""){
            $b_valid = false;
         }
    }
    if (!isset($_POST['location_name']) || $_POST['location_name'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['location_address']) || $_POST['location_address'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['location_city']) || $_POST['location_city'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['location_state']) || $_POST['location_state'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['location_zip']) || $_POST['location_zip'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['location_phone']) || $_POST['location_phone'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['user_firstname']) || $_POST['user_firstname'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['user_lastname']) || $_POST['user_lastname'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['user_username']) || $_POST['user_username'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['user_email']) || $_POST['user_email'] != "") == ""){
        $b_valid = false;
    }
    elseif  (!isset($_POST['user_password']) || $_POST['user_password'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['user_mobile']) || $_POST['user_mobile'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['payment_cc_name']) || $_POST['payment_cc_name'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['payment_cc_number']) || $_POST['payment_cc_number'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['payment_cc_expo_month']) || $_POST['payment_cc_expo_month'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['payment_cc_expo_year']) || $_POST['payment_cc_expo_year'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['payment_cc_code']) || $_POST['payment_cc_code'] == ""){
        $b_valid = false;
    }
    elseif (!isset($_POST['terms']) || $_POST['terms'] == "")
        $b_valid = false;
    }
    return $b_valid;
}

now it's easy to modify / read etc. Because empty can be slightly ambiguous I find myself avoiding it as a rule, despite it's stylistic elegance. 现在它很容易修改/阅读等等。因为空洞可能有点模棱两可,我发现自己通常会避开它,尽管它具有风格优雅。

To make this even cleaner, I'd probably write it like this: 为了使这更清洁,我可能会这样写:

function validate_input($billingRequired=0){
    $b_valid = true;
    $a_billing = array('billing_company','billing_address'...);
    $a_main = array('billing_address','location_address'...);
    if( $billingRequired == 1 ){
         $a_main = array_merge($a_billing,$a_main);
    }
    foreach ($a_main as $test){
       if (!isset($_POST[$test]) || trim($_POST[$test]) == "")
            $b_valid = false;
            break;
        }
    }
    return $b_valid;
}

With the caveat that assumes empty values are "", which wouldn't generally be the case for select lists etc. 假设空值为“”,这通常不是选择列表等的情况。

What I would do is create an array at the top of the script that contains a list of all the POSTed variables that are required, then I would just loop through them. 我要做的是在脚本的顶部创建一个数组,其中包含所需的所有POSTed变量的列表,然后我将循环遍历它们。 Your code would be way smaller. 你的代码会更小。 and almost as concise.... 而且几乎一样简洁......

//We NEED these fields for the script to work...
$requiredFields = array(
    "fname",
    "lname",
    "phone",
    "accredited",
    "etc, etc"
);

//IF you require billing, 
if($billingRequired){
    // Define the billing fields that we expect to be POSTed...
    $billingFields  = array(
        "billing_compnay",
        "billing_address",
        "etc, etc"
    );
    // Add the billing fields to the required fields
    $requiredFields = array_merge($requiredFields, $billingFileds);
}

// Loop through required fields and check to see if they are all POSTed
foreach($requiredFields as $fieldName){
    // IF a required field is not set...
    if(empty($_POST[$fieldName])){
        // Do stuff, call a function, show an error, etc.
        break; // Or redirect, or exit after a JSON response, whatever. Just be sure to end the loop here for efficiency.
    }
}

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

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