php mariadb sum除以若沒有值,會出現NAN,能否顯示0?

$selectTip = $pdo->query("SELECT * FROM `tips_rate`
                WHERE `tip_id` = '".$row['id']."' "
              );
              $selecttotal = mysqli_num_rows($selectTip);
              $rate = $pdo->query("SELECT SUM(star) as startotal FROM `tips_rate`
                WHERE `tip_id` = '".$row['id']."' "
              );
              $startotal = mysqli_fetch_array($rate);
              echo $startotal['startotal']/$selecttotal;

當我echo他們互除以的結果後
假設沒有值他會出現NAN...能不能顯示 0?

慕尼黑5688855
浏览 365回答 1
1回答

翻翻过去那场雪

$num = $startotal['startotal']/$selecttotal;echo is_nan($num) ? 0 : $num;
打开App,查看更多内容
随时随地看视频慕课网APP