0
votes

im trying to do a calculation of variables but my answer is coming out with an error (Warning: Division by zero in), i assume its because by default my variables are actually Zero.

<?php
    $tax = $row['tax']; //Pulls Tax Data
    $totalPayableSites = $row3['total']; // pulls total sites payable
    $iskPerRun1 = $sum_total - $tax;
    $iskPerRun2 = $iskPerRun1/$totalPayableSites;
    $iskPerRunRound = floor($iskPerRun2 / 100) * 100;
    $iskPerRun = number_format($iskPerRunRound);

    if (($iskPerRunRound) == 0) {
        echo "0 ISK"; }
    else {
        echo $iskPerRun;
        echo " ISK"; }
?>

how can i re-write this to check if its zero and only run calculations if its not zero?

apart from that the code works flawless when my variables are not "0"

All variables are "0" by default before the script is edited.

2
Why don't you use an if statement ?Clément Prévost

2 Answers

1
votes

Loads of ways to do this I guess, here's one (assuming you want a positive value):

if($totalPayableSites > 0){
  $iskPerRun2 = $iskPerRun1/$totalPayableSites;
  $iskPerRunRound = floor($iskPerRun2 / 100) * 100;
  $iskPerRun = number_format($iskPerRunRound);
  echo $iskPerRun;
  echo " ISK";
}else{
  echo "0 totalPayableSites";
}
0
votes

Why don't you just check for 0 before dividing, then?

    $iskPerRun2 = $totalPayableSites ? $iskPerRun1/$totalPayableSites : 0;

for instance