如果在php中,嵌套的if语句在另一个内部

时间:2018-03-05 19:21:21

标签: php wordpress

我正在尝试编写一个函数,如果价格不为零,则应检查变量price_label_before为零,然后是否应返回某种格式或者返回其他内容。

E.g 1

price_label_before =美元 货币= KES 价格= 2000

返回2000美元

例如2 price_label_before = null 货币= KES 价格= 2000

返回KES 2000

以下是代码:

$currency       =   esc_html( get_option('wp_estate_currency_symbol', '') );

$where_currency =   esc_html( get_option('wp_estate_where_currency_symbol', '') );

$price_label_before    =   floatval ( get_post_meta($post_id, 'property_label_before', true) );  

$price  = floatval   ( get_post_meta($post_id, 'property_price', true) );

if ($price != 0 ) {

    if ($price_label_before = 0) {


    $price =wpestate_show_price($post_id,$currency,$where_currency,1);   
     }
     else {
           $myprice = floatval   ( get_post_meta($post_id, 'property_price', true) );

           $price='<span class="price_label price_label_before1">'.$price_label_before. '&nbsp;' .number_format($myprice).'</span><span class="price_label ">'.$price_label.'</span>';  
     }             

}else{

    $price='';

} 

1 个答案:

答案 0 :(得分:1)

您的嵌套else条件永远不会得到满足,因为您在if语句中使用了赋值运算符。

if ($price_label_before = 0) {

应改为: if ($price_label_before == 0) {

修改:更新以反映empty条件而不是0条件

if ($price_label_before == '' ) {

或者:

if ( empty( $price_label_before ) ) {

现在,设置 $price_label_before为0,然后检查它是否为0(总是如此)

这是Assignment OperatorComparison Operator之间的差异(特别是相等运算符)

作为(与问题部分无关)的旁注,我会尝试使用间距和空格来处理代码一致性,以使代码更清晰。看起来你的代码中有很多随机空格和中断,这会使你的代码库变得越来越大,难以阅读和维护。

$currency       =   esc_html( get_option( 'wp_estate_currency_symbol', '' ) );
$where_currency =   esc_html( get_option( 'wp_estate_where_currency_symbol', '' ) );

$price_label_before = floatval( get_post_meta( $post_id, 'property_label_before', true ) );  
$price              = floatval( get_post_meta( $post_id, 'property_price', true ) );

if( $price != 0 ){
    if( $price_label_before == '' ){
        $price = wpestate_show_price( $post_id, $currency, $where_currency, 1 );   
    } else {
        $myprice = floatval( get_post_meta( $post_id, 'property_price', true ) );
        $price   = '<span class="price_label price_label_before1">'. $price_label_before .'&nbsp;'. number_format( $myprice ) .'</span><span class="price_label ">'. $price_label .'</span>';  
    }
} else {
    $price = '';
} 
相关问题