PHP警告:除数为零

编程入门 行业动态 更新时间:2024-10-27 14:23:56
本文介绍了PHP警告:除数为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习php,并建立了一个实验性的基于表单的计算器(也使用html& POST方法),该计算器将值返回到表中.输入值并单击提交"时,计算器将起作用,但是在我第一次运行代码时,在最后一行仍然出现两个被零除"错误.在此处或通过Google搜索时,似乎似乎找不到合乎逻辑的解决方案或解释.您可以提供给newb的任何解释将不胜感激.

I'm learning php and built an experimental form-based calculator (also using html & POST method) that returns values to a table. The calculator is functional when I enter my values and click submit, but I keep getting two "Division by zero" errors on the last line when I first run the code. I can't seem seem to find a logical solution or explanations when searching here or via Google. Any explanation you can provide to a newb will be appreciated.

<?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 ; ?>

推荐答案

您需要将表单处理代码包装在条件代码中,这样在您第一次打开页面时它就不会运行.像这样:

You need to wrap your form processing code in a conditional so it doesn't run when you first open the page. Something like 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 ; }

更多推荐

PHP警告:除数为零

本文发布于:2023-11-26 19:38:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:除数   为零   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!