繁体   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