[英]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.