-6

I'm getting a warning "Division by zero"

this is the code

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

i guess i get it only when the kills and death values both are zero Is there any possible solution

Prabin
  • 1
  • 2

1 Answers1

2

Just add an if statement before division

<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>
Ismail Zafar
  • 174
  • 1
  • 11