PHP Warning: Division by zero

后端 未结 8 1234
温柔的废话
温柔的废话 2020-12-20 14:46

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 ent

8条回答
  •  情歌与酒
    2020-12-20 15:07

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

提交回复
热议问题