phpbeginner Posted November 3, 2006 Share Posted November 3, 2006 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 More sharing options...
obsidian Posted November 3, 2006 Share Posted November 3, 2006 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]<?phpif ($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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.