Natloy90 Posted April 5, 2008 Share Posted April 5, 2008 Im running an online text based game and recently done some core edits. I've run into an error and can't seem to find the problem, the error im getting is: PHP Warning: "Division by zero" in /home/f/i/firestorms/public_html/modules/healthbar.php at 52. Call Stack: 2: healthbar_dohook("charstats", Array( "available"=>10, "used"=>0 )) called from /home/f/i/firestorms/public_html/lib/modules.php on line 514 3: modulehook("charstats") called from /home/f/i/firestorms/public_html/lib/pageparts.php on line 692 4: charstats() called from /home/f/i/firestorms/public_html/lib/pageparts.php on line 124 5: page_footer() called from /home/f/i/firestorms/public_html/newday.php on line 323 My code is: function healthbar_dohook($hookname,$args){ global $session; switch($hookname){ case "charstats": if ($session['user']['alive']) { $cur = $session['user']['hitpoints']; $realmax = $session['user']['maxhitpoints']; $stat = "Hitpoints"; $cur_adjustment = check_temp_stat("hitpoints",1); $max_adjustment = check_temp_stat("maxhitpoints",1); } else { $cur = $session['user']['soulpoints']; $realmax = $session['user']['level'] * 5 + 50; $stat = "Soulpoints"; $cur_adjustment = check_temp_stat("soulpoints",1); $max_adjustment = ""; } if ($cur > $realmax) $max = $cur; else $max = $realmax; $pct = round($cur / $max * 100, 0); $nonpct = 100-$pct; if ($pct > 100) { $pct = 100; $nonpct = 0; } if ($pct < 0) { $pct = 0; $nonpct = 100; } if ($pct > 60) { if ($session['user']['alive']) $color = "#00ff00"; else $color = "#dddddd"; $ccode = "`@"; } elseif ($pct > 25) { if ($session['user']['alive']) $color = "#ffff00"; else $color = "#666666"; $ccode = "`^"; } else { if ($session['user']['alive']) $color = "#ff0000"; else $color = "#880000"; $ccode = "`$"; } $hicode = "`&"; if (!$session['user']['alive']) { $ccode = "`7"; } The Bold piece of the code is the line of 'error' in /home/f/i/firestorms/public_html/modules/healthbar.php at 52. Link to comment https://forums.phpfreaks.com/topic/99693-php-warning-division-by-zero/ Share on other sites More sharing options...
fanfavorite Posted April 5, 2008 Share Posted April 5, 2008 Appears that $cur and/or $max are the wrong data type. Make sure that they are integers to use round. You can use intval (http://ca.php.net/intval) to get the integer value of a string. Link to comment https://forums.phpfreaks.com/topic/99693-php-warning-division-by-zero/#findComment-509999 Share on other sites More sharing options...
Barand Posted April 5, 2008 Share Posted April 5, 2008 PHP Warning: "Division by zero" As it says on the tin - you are trying to divide by zero, so $max must be zero when it fails. Link to comment https://forums.phpfreaks.com/topic/99693-php-warning-division-by-zero/#findComment-510008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.