繁体   English   中英

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

[英]In PHP how do you do an if statement within the if of an 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'] != "")
){

但是,根据一些变量,我可能不再需要任何计费信息。 所以我想知道我是否可以在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'] != "")
){

我很确定没有,但想与比我更聪明的人检查。 我知道我可以在{}旁边做一些嵌套但是不想在非常深的嵌套中检查每个变量。

谢谢,

只需在要组合的部分周围添加(),它就会解析为一个简单的布尔值,它可以包含在你的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'] != "")
    )
{
    //statements
}

虽然我怀疑这可以在一个循环中做得更好,也许这是个人偏好,但我会更喜欢这样的事情

$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
}

更好的是,不要在主条件中放置这么多测试,只需要做一个函数来测试它,然后测试=== 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;
}

现在它很容易修改/阅读等等。因为空洞可能有点模棱两可,我发现自己通常会避开它,尽管它具有风格优雅。

为了使这更清洁,我可能会这样写:

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;
}

假设空值为“”,这通常不是选择列表等的情况。

我要做的是在脚本的顶部创建一个数组,其中包含所需的所有POSTed变量的列表,然后我将循环遍历它们。 你的代码会更小。 而且几乎一样简洁......

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