简化PHP中很长的if语句

简化PHP中很长的if语句,php,if-statement,Php,If Statement,当我提交表单时,我只想用N/A设置空字段。 我的代码正在运行,但我认为它太长了,有没有办法简化它?用检查空白,然后这样写: $gateway_site_id = $_POST['gateway_site_id']; $site_location = $_POST['site_location']; $city = $_POST['city']; $contact = $_POST['gateway_site_id']; $date_installed = $_POST['date_install

当我提交表单时,我只想用N/A设置空字段。 我的代码正在运行,但我认为它太长了,有没有办法简化它?

用检查空白,然后这样写:

$gateway_site_id = $_POST['gateway_site_id'];
$site_location = $_POST['site_location'];
$city = $_POST['city'];
$contact = $_POST['gateway_site_id'];
$date_installed = $_POST['date_installed'];
$care_of = $_POST['care_of']; 
$notes = $_POST['notes'];
$gateway_username = $_POST['gateway_username'];



if(empty($gateway_username)){
    $gateway_username="N/A";
}if(empty($notes)){
    $notes="N/A";
}if(empty($care_of)){
    $care_of="N/A";
}if(empty($date_installed)){
    $date_installed="N/A";
}if(empty($city)){
    $city="N/A";
}if(empty($site_location)){
    $site_location="N/A";
}if(empty($gateway_site_id)){
    $gateway_site_id="N/A";
}
$gateway_site_id    = $_POST['gateway_site_id'] ?? 'N/A';
$site_location      = $_POST['site_location']  ?? 'N/A';
$city               = $_POST['city'] ?? 'N/A';
$contact            = $_POST['gateway_site_id'] ?? 'N/A';
$date_installed     = $_POST['date_installed'] ?? 'N/A';
$care_of            = $_POST['care_of'] ?? 'N/A';
$notes              = $_POST['notes'] ?? 'N/A';
$gateway_username   = $_POST['gateway_username'] ?? 'N/A';
检查空白,并这样写:

$gateway_site_id = $_POST['gateway_site_id'];
$site_location = $_POST['site_location'];
$city = $_POST['city'];
$contact = $_POST['gateway_site_id'];
$date_installed = $_POST['date_installed'];
$care_of = $_POST['care_of']; 
$notes = $_POST['notes'];
$gateway_username = $_POST['gateway_username'];



if(empty($gateway_username)){
    $gateway_username="N/A";
}if(empty($notes)){
    $notes="N/A";
}if(empty($care_of)){
    $care_of="N/A";
}if(empty($date_installed)){
    $date_installed="N/A";
}if(empty($city)){
    $city="N/A";
}if(empty($site_location)){
    $site_location="N/A";
}if(empty($gateway_site_id)){
    $gateway_site_id="N/A";
}
$gateway_site_id    = $_POST['gateway_site_id'] ?? 'N/A';
$site_location      = $_POST['site_location']  ?? 'N/A';
$city               = $_POST['city'] ?? 'N/A';
$contact            = $_POST['gateway_site_id'] ?? 'N/A';
$date_installed     = $_POST['date_installed'] ?? 'N/A';
$care_of            = $_POST['care_of'] ?? 'N/A';
$notes              = $_POST['notes'] ?? 'N/A';
$gateway_username   = $_POST['gateway_username'] ?? 'N/A';

如果您使用的是PHP 7,则可以使用合并运算符检查POST数组中的空值,如:

$gateway_site_id = $_POST['gateway_site_id'] ?: 'N/A' ;
// ...
$gateway_username = $_POST['gateway_username'] ?: 'N/A';

// all `if`s can be removed

如果您使用的是PHP 7,则可以使用合并运算符检查POST数组中的空值,如:

$gateway_site_id = $_POST['gateway_site_id'] ?: 'N/A' ;
// ...
$gateway_username = $_POST['gateway_username'] ?: 'N/A';

// all `if`s can be removed
大概是这样的:

$gateway_site_id = $_POST['gateway_site_id'];
$site_location = $_POST['site_location'];
$city = $_POST['city'];
$contact = $_POST['gateway_site_id'];
$date_installed = $_POST['date_installed'];
$care_of = $_POST['care_of']; 
$notes = $_POST['notes'];
$gateway_username = $_POST['gateway_username'];



if(empty($gateway_username)){
    $gateway_username="N/A";
}if(empty($notes)){
    $notes="N/A";
}if(empty($care_of)){
    $care_of="N/A";
}if(empty($date_installed)){
    $date_installed="N/A";
}if(empty($city)){
    $city="N/A";
}if(empty($site_location)){
    $site_location="N/A";
}if(empty($gateway_site_id)){
    $gateway_site_id="N/A";
}
$gateway_site_id    = $_POST['gateway_site_id'] ?? 'N/A';
$site_location      = $_POST['site_location']  ?? 'N/A';
$city               = $_POST['city'] ?? 'N/A';
$contact            = $_POST['gateway_site_id'] ?? 'N/A';
$date_installed     = $_POST['date_installed'] ?? 'N/A';
$care_of            = $_POST['care_of'] ?? 'N/A';
$notes              = $_POST['notes'] ?? 'N/A';
$gateway_username   = $_POST['gateway_username'] ?? 'N/A';
大概是这样的:

$gateway_site_id = $_POST['gateway_site_id'];
$site_location = $_POST['site_location'];
$city = $_POST['city'];
$contact = $_POST['gateway_site_id'];
$date_installed = $_POST['date_installed'];
$care_of = $_POST['care_of']; 
$notes = $_POST['notes'];
$gateway_username = $_POST['gateway_username'];



if(empty($gateway_username)){
    $gateway_username="N/A";
}if(empty($notes)){
    $notes="N/A";
}if(empty($care_of)){
    $care_of="N/A";
}if(empty($date_installed)){
    $date_installed="N/A";
}if(empty($city)){
    $city="N/A";
}if(empty($site_location)){
    $site_location="N/A";
}if(empty($gateway_site_id)){
    $gateway_site_id="N/A";
}
$gateway_site_id    = $_POST['gateway_site_id'] ?? 'N/A';
$site_location      = $_POST['site_location']  ?? 'N/A';
$city               = $_POST['city'] ?? 'N/A';
$contact            = $_POST['gateway_site_id'] ?? 'N/A';
$date_installed     = $_POST['date_installed'] ?? 'N/A';
$care_of            = $_POST['care_of'] ?? 'N/A';
$notes              = $_POST['notes'] ?? 'N/A';
$gateway_username   = $_POST['gateway_username'] ?? 'N/A';
试试这个:

试试这个: