我正在学习PHP并构建了一个基于表单的实验计算器(也使用html& POST方法),它将值返回给表.当我输入我的值并点击提交时,计算器正常运行,但是当我第一次运行代码时,我在最后一行上得到两个“除以零”错误.在这里或通过Google搜索时,我似乎无法找到合理的解决方案或解释.您可以向newb提供任何解释,我们将不胜感激.
<?PHP error_reporting(E_ALL ^ E_NOTICE); //calculate the 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 ; ?>
您需要将表单处理代码包装在条件中,以便在首次打开页面时不会运行.像这样的东西:
原文链接:https://www.f2er.com/php/136035.htmlif($_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 ; }