PHP Warning: Division by zero -
i'm learning php , built experimental form-based calculator (also using html & post method) returns values table. calculator functional when enter values , click submit, keep getting 2 "division zero" errors on last line when first run code. can't seem seem find logical solution or explanations when searching here or via google. explanation can provide newb appreciated.
<?php error_reporting(e_all ^ e_notice); //calculate difference in price $itemqty = $_post['num1']; $itemcost = $_post['num2']; $itemsale = $_post['num3']; $shipmat = $_post['num4']; $diffprice = $itemsale - $itemcost; $actual = ($diffprice - $shipmat) * $itemqty; $diffpricepercent = (($actual * 100) / $itemcost) / $itemqty ; ?>
you need wrap form processing code in conditional doesn't run when first open page. so:
if($_post['num1'] > 0 && $_post['num2'] > 0 && $_post['num3'] > 0 && $_post['num4'] > 0){ $itemqty = $_post['num1']; $itemcost = $_post['num2']; $itemsale = $_post['num3']; $shipmat = $_post['num4']; $diffprice = $itemsale - $itemcost; $actual = ($diffprice - $shipmat) * $itemqty; $diffpricepercent = (($actual * 100) / $itemcost) / $itemqty ; }
Comments
Post a Comment