PHP 条件语句:如果数据库字段等于“0.0”,则必须隐藏到页面


Php Conditional statement : if database field equal "0.0" must hide to the page

在我的数据库中,我将我的一些字段设置为decimal这样我就可以输入具有小数点的数字。

在我的数据库中,即使我不在输入字段中输入任何内容,也始终具有 0.0 的值。

就我而言,我不希望0.0显示在我的页面中,所以我尝试这样做:

 <?php 
    if(!empty($current_orders["order_item"])){
        echo "<p>Credit Amount:  <span class='"colorRed'">Php ". $current_orders["order_item"]."</span></p>";
    } else {
        echo null;
    }
  ?>

而且它不起作用...

不为空表示这些变量具有值。 空表示这些变量为空。0.0 是一个值。因此,将您的代码更改为以下内容:

<?php if($current_orders["order_item"] > 0.0){
  echo "<p>Credit Amount:  <span class='"colorRed'">Php " 
   .  $current_orders["order_item"] . "</span></p>";
    } else {
    echo null;
  }
?>

您可以在 php 中使用 bccomp 函数来比较浮点值。

如果两个操作数相等,则返回 0,如果left_operand大于 right_operand则返回 1,否则返回 -1

<?php 
if(bccomp($current_orders["order_item"],0.0,1) == 0){
    echo "<p>Credit Amount:  <span class='"colorRed'">Php ". $current_orders["order_item"]."</span></p>";
} else {
    echo null;
}

?>

您可以使用以下代码摆脱此问题

$amount =   0.0;
if((!empty($amount)) && $amount>0){
    echo "<p>Credit Amount:  <span class='"colorRed'">Php ". $amount."</span></p>";
} else {
    echo null;
}

如果要显示 -ve 值,则只需将条件 $amount>0 更改为 $amount!=0,否则它不会显示 -ve 值