当值为零时除以零

时间:2015-10-11 15:55:00

标签: php

我收到警告“除以零”

这是代码

<p class='text-alert' id ="greet" align="left">Kill Death Ratio : <?php
$kd = $data['kills'] / $data['deaths'];
echo round($kd, 2);
?></p>

我想我只有在杀戮和死亡价值均为零时才能得到它 有没有可能的解决方案

1 个答案:

答案 0 :(得分:2)

只需在分区前添加if语句

<p class='text-alert' id ="greet" align="left">Kill Death Ratio : <?php
if  ($data['deaths'] != 0){
$kd = $data['kills'] / $data['deaths'];
echo round($kd, 2);
}
else{
// Some other code which you want to run in case of zero
}
?></p>