Search code examples
phpdivide-by-zero

How to bypass a Divide by zero warning in php?


I have a simple division calculation after I've taken data from a MYSQL database

$calculation = round($number1/$number2,2);

Sometimes $number2 comes out the database as a 0 and I get a divide by zero error, is there a way to prevent the divide by zero warning and show a zero by default?


Solution

  • $gpg = $games == 0 ? 0 : round($goals / $games, 2);
    $mpg = $goals == 0 ? 0 : round($minutes / $goals);