PHP警告:除以零

栏目: PHP · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/18408640/php-warning-division-by-zero
我正在学习 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 ;

?>
您需要将表单处理代码包装在条件中,以便在首次打开页面时不会运行.像这样的东西:
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 ;
}

翻译自:https://stackoverflow.com/questions/18408640/php-warning-division-by-zero


以上所述就是小编给大家介绍的《PHP警告:除以零》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Caching

Web Caching

Duane Wessels / O'Reilly Media, Inc. / 2001-6 / 39.95美元

On the World Wide Web, speed and efficiency are vital. Users have little patience for slow web pages, while network administrators want to make the most of their available bandwidth. A properly design......一起来看看 《Web Caching》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具