tvirelli tvirelli - 1 month ago 7
PHP Question

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(
(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(
($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,

Answer

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, like this

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'] != "") && 
    ))
    ||
    (($billingRequired != 1) && (
        (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');
$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;
}

if ($continue) {
    // statements
}
Comments