Jump to content

Division By 0 Error


phpbeginner

Recommended Posts

I have a hockey program and I have a "Division by 0" error which occurs on pages where the input is 0.

To calculate accurate GAA I use this code:
[code]$pcent = round('1.00' * ($p_array[$playerid]['goalsa']) * (60/$p_array[$playerid]['minutes']), 2);
$p_array[$playerid]['gaa'] = number_format($pcent, 2, '.', ',');[/code]

This works great except that if a Goalie has yet to play any minutes, I get a "division by 0 error". Is there a simple solution to this ?
Link to comment
https://forums.phpfreaks.com/topic/26052-division-by-0-error/
Share on other sites

Just run a check before your calculation to see whether or not they have played. If not, come up with a default value you can show:
[code]
<?php
if ($p_array[$playerid]['minutes'] < 1) {
  // player has less than one minute of playtime, so we don't calculate
} else {
  $pcent = round('1.00' * ($p_array[$playerid]['goalsa']) * (60/$p_array[$playerid]['minutes']), 2);
  $p_array[$playerid]['gaa'] = number_format($pcent, 2, '.', ',');
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26052-division-by-0-error/#findComment-119101
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.