I am working on a Hockey Pool stat project. My PPG column is a calculation that divides PTS by GP. This works fine for anyone that has played a game. However, in the case of Rookies or the very beginning of the season the GP number is 0. As such, when it is "0" it returns a DIVISION BY ZERO error. Instead of this error, I would like to simply return the result to be "0.00". I have not had any success finding a solution. Hoping someone can help!

Here is the code that I have been using for this column of my query output.

echo number_format($row['PTS0910']/$row['GP0910'],2);

thanks in advance everyone!
Cheers,
Chris

Recommended Answers

All 2 Replies

The division by zero error is only a warning so it doesn't stop execution. You can prevent the warning from printing (if necessary) using error_reporting (E_ERROR);

It looks as if the division by zero returns false rather than a numeric value. Thus, your code could be changed as follows:

$result = $row['PTS0910']/$row['GP0910'];
if ($result == false) {
   $result = 0.00;
}

echo  number_format($result,2);

Thank you so much. this is exactly what I was looking for. The error_reporting (E_ERROR); was the answer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.