drxpt06820 2016-09-22 16:02
浏览 35
已采纳

PHP如果$ var null则返回$ var2

I havent been able to find an answer to this:

My code works like this:

echo ' <p> '.$test2.'</p> ';

I want to make $test2 change if it is null i have tried the following code and bunch of other and it just breaks the page

$test2 = if(empty($price)){ return $pstatus};

Any help would be apprteciated

UPDATE I have modified my code, now it returns the price but if the price is empty its returning nothing when it should return the $pstatus

so $price is say 625000 is that variable is empty it should return sold from the $pstatus variable

$price = number_format($p->meta_box->REAL_HOMES_property_price);
$fimage = $p->better_featured_image->media_details->sizes->medium->source_url;
$ptitle = $p->title->rendered;
$pdesc = $p->excerpt->rendered;
$plink = $p->link;
$pstatus = $p->pure_taxonomies->{"property-status"}[0]->name;
$test2 = $pstatus ? $price : $test2;
  • 写回答

2条回答 默认 最新

  • dptgpyl61857413 2016-09-22 16:05
    关注

    You can do it inline:

    $test2 = $price ? $pstatus : $test2;
    

    If you need to assign "$price" to "$test2" and if there is no price then assign "$pstatus":

    $test2 = $price ? $price : $pstatus;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码