gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:16:33 26 4
gpt4 key购买 nike

所以我知道这个问题令人困惑。这就是我想知道的方法。

我有以下 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'] != "")
){

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

谢谢,

最佳答案

只需在您要合并的部分周围添加 (),它将解析为一个简单的 bool 值,可以包含在您的 if 语句中

这里的逻辑是“如果(不需要billing或者billing字段都填写了)并且所有非billing字段都填写了,那么...”

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
}

关于php - 在 PHP 中,如何在 if 语句的 if 中执行 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39965462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com