在PHP中设置/使用NULL值的正确方法';大于或等于';否则


Correct Method for setting/using a NULL value in PHP 'Greater than or equal' IF ELSE

我有一个简单的if-else语句,想知道是否有比下面列出的更好的代码编写方法,$price_data和$api_price通常是设置的,但在某些情况下可能是空值,所以如果有空值,我需要恢复到$old_price

// now double the price compare api price and current price use highest value
if ($price_data >= $api_price) {
$price_double = $price_data; 
// If we encounter a null or empty value for $price_data use old price data
  if ($price_double == null){ 
      $price_double = $old_price; 
  }
} else {
$price_double = $api_price;  
// If we encounter a null or empty value for $price_data use old price data
  if ($price_double == null){ 
      $price_double = $old_price; 
  }   
}
$useprice = ($price_double * 2);

我认为这会达到你想要的效果:

if ((floatval($price_data) === 0) || (floatval($api_price) === 0)):
    $price_double = $old_price;
else:
    $price_double = ($price_data >= $api_price)?$price_data:$api_price;
endif;