簡體   English   中英

檢查值是否為空

[英]checking if value is empty

我正在嘗試修補一些在wp函數文件中輸出日期范圍的代碼。 這是生成的代碼:

function date_flip() {
    global $custom_metabox; //needed    
    $start_date = $custom_metabox->get_the_value('start_date');
    $end_date   = $custom_metabox->get_the_value('end_date');
    //start day
    $s_day   = substr($start_date,8,2);
    $s_month = substr($start_date,5,2);
    $s_year  = substr($start_date,0,4);
    //end day
    $e_day   = substr($end_date,8,2);
    $e_month = substr($end_date,5,2);
    $e_year  = substr($end_date,0,4);
    //mash up
    $start_date = $s_day . "." . $s_month . "." . $s_year;
    $end_date   = $e_day . "." . $e_month . "." . $e_year;
    //spew out
    echo $start_date . ' – ' . $end_date;
}

我的問題是,有時我們不在帖子中添加end_date,因此此代碼輸出一組空白的數字,例如9.9.2015–..

我試圖通過不使用檢查是否為空值來實現$ end_date來縮小范圍,但最終卻只是掛斷了我的網站。

我用這個替換了最后一個回聲:

function date_flip() {
    global $custom_metabox; //needed    
    $start_date = $custom_metabox->get_the_value('start_date');
    $end_date   = $custom_metabox->get_the_value('end_date');
    //start day
    $s_day   = substr($start_date,8,2);
    $s_month = substr($start_date,5,2);
    $s_year  = substr($start_date,0,4);
    //end day
    $e_day   = substr($end_date,8,2);
    $e_month = substr($end_date,5,2);
    $e_year  = substr($end_date,0,4);
    //mash up
    $start_date = $s_day . "." . $s_month . "." . $s_year;
    $end_date   = $e_day . "." . $e_month . "." . $e_year;
    //spew out
    echo $start_date;
    if ( !empty( $custom_metabox->get_the_value('end_date') ) ) {
        echo ' – ' . $end_date;
    } else {
        //DO NOTHING
    }

任何線索為什么這可能會掛斷整個網站?

缺少) -

if ( !empty( $custom_metabox->get_the_value( 'end_date' ) ) ) {

您必須將代碼替換為下面的代碼。 缺少括號

global $custom_metabox;
echo $start_date;
if ( !empty( $custom_metabox->get_the_value('end_date') ) ) {
    echo ' – ' . $end_date;
} else {
    //DO NOTHING
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM