PHP 警告:除以零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18408640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Warning: Division by zero
提问by Chip
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 并构建了一个基于表单的实验计算器(也使用 html 和 POST 方法),该计算器将值返回到表中。当我输入我的值并单击提交时,计算器可以正常工作,但是当我第一次运行代码时,我在最后一行不断收到两个“被零除”错误。在此处或通过 Google 搜索时,我似乎无法找到合乎逻辑的解决方案或解释。您可以向新手提供的任何解释将不胜感激。
<?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 ;
?>
采纳答案by S.A
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 ;
}
回答by Hussaini Ibrahim
Try using
尝试使用
$var = @($val1 / $val2);
回答by Ahmed Alaa El-Din
$diffPricePercent = (($actual * 100) / $itemCost) / $itemQty;
$itemCost
and $itemQty
are returning null
or zero, check them what they come with to code from user input
$itemCost
并$itemQty
返回null
或为零,检查他们从用户输入中编码的内容
also to check if it's not empty data add:
还要检查它是否不是空数据添加:
if (!empty($_POST['num1'])) {
$itemQty = $_POST['num1'];
}
and you can check this link for POST validation before using it in variable
在变量中使用它之前,您可以检查此链接以进行 POST 验证
https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
回答by Naftali aka Neal
If a variable is not set then it is NULL
and if you try to divide something by null you will get a divides by zero error
如果一个变量没有设置,那么它是NULL
,如果你试图除以 null,你会得到一个被零除的错误
回答by Shomz
If it shows an error on the first run only, it's probably because you haven't sent any POST data. You should check for POST variables before working with them. Undefined, null, empty array, empty string, etc. are all considered false; and when PHP auto-casts that false boolean value to an integer or a float, it becomes zero. That's what happens with your variables, they are not set on the first run, and thus are treated as zeroes.
如果仅在第一次运行时显示错误,则可能是因为您尚未发送任何 POST 数据。在使用它们之前,您应该检查 POST 变量。undefined、null、空数组、空字符串等都被认为是false;当 PHP 将该 false 布尔值自动转换为整数或浮点数时,它会变成零。这就是您的变量发生的情况,它们不是在第一次运行时设置的,因此被视为零。
10 / $unsetVariable
becomes
变成
10 / 0
Bottom line: check if your inputs exist and if they are valid before doing anything with them, also enable error reporting when you're doing local work as it will save you a lot of time. You can enable all errors to be reported like this: error_reporting(E_ALL);
底线:在对它们进行任何操作之前检查您的输入是否存在以及它们是否有效,同时在您进行本地工作时启用错误报告,因为这将为您节省大量时间。您可以像这样启用所有错误报告:error_reporting(E_ALL);
To fix your specific problem: don't do any calculations if there's no input from your form; just show the form instead.
解决您的具体问题:如果您的表单没有输入,请不要进行任何计算;只需显示表格即可。
回答by Sairen
when my divisor has 0 value
当我的除数为 0 时
if ($divisor == 0) {
$divisor = 1;
}
回答by UWU_SANDUN
You can try with this. You have this error because we can not divide by 'zero' (0)
value. So we want to validate before when we do calculations.
你可以试试这个。你有这个错误,因为我们不能除以'zero' (0)
值。所以我们要在计算之前验证。
if ($itemCost != 0 && $itemCost != NULL && $itemQty != 0 && $itemQty != NULL)
{
$diffPricePercent = (($actual * 100) / $itemCost) / $itemQty;
}
And also we can validate POST
data. Refer following
我们也可以验证POST
数据。参考以下
$itemQty = isset($_POST['num1']) ? $_POST['num1'] : 0;
$itemCost = isset($_POST['num2']) ? $_POST['num2'] : 0;
$itemSale = isset($_POST['num3']) ? $_POST['num3'] : 0;
$shipMat = isset($_POST['num4']) ? $_POST['num4'] : 0;
回答by Rahul
try this
尝试这个
if(isset($itemCost) != '' && isset($itemQty) != '')
{
$diffPricePercent = (($actual * 100) / $itemCost) / $itemQty;
}
else
{
echo "either of itemCost or itemQty are null";
}