Division by zero in PHP, as a warning and as an exception
Warning: Division by zeroShort answer
In PHP 7 the / operator warned on a zero divisor. Since PHP 8.0 it throws DivisionByZeroError, as % and intdiv() already did, so the script stops unless you catch it. Check the divisor before dividing, use fdiv() when you want INF or NAN, or catch DivisionByZeroError.
Fix it fast
1Find out which cause you have
Confirm which PHP runs the code. On PHP 8 the second command prints the exception message; on PHP 7 it prints a warning. Then read the line in your stack trace and look at the divisor.
php -v php -r 'try { echo 1 / 0; } catch (DivisionByZeroError $e) { echo $e->getMessage(), PHP_EOL; }'2Match what you see, then jump to its fix
- 1
Uncaught DivisionByZeroError: Division by zero, after upgrading from PHP 7The / operator got a zero divisor on PHP 8Fix - 2
DivisionByZeroError on a line that uses % or intdiv()A zero reaches % or intdiv()Fix - 3
Code from PHP 7 relied on INF or NAN coming back after the warningYou want IEEE results (INF or NAN), not an exceptionFix
- 1
What does the error look like?
Warning: Division by zeroFatal error: Uncaught DivisionByZeroError: Division by zero
Cause 1 of 3
The / operator got a zero divisor on PHP 8
1Check
On PHP 7 the same code only produced a Warning and a result of INF or NAN. The change to throw DivisionByZeroError was accepted for PHP 8.0, so code that ran quietly now stops.
php -r 'try { echo 1 / 0; } catch (DivisionByZeroError $e) { echo get_class($e), ": ", $e->getMessage(), PHP_EOL; }'2Fix
Check the divisor first and decide what a zero should mean for the page, a default value or an error message.
$average = $count > 0 ? $total / $count : 0;3Verify
Run the code path with a zero divisor and confirm no exception is thrown.
php -r '$total = 10; $count = 0; echo $count > 0 ? $total / $count : 0, PHP_EOL;'Confirmed by wiki.php.net
Cause 2 of 3
A zero reaches % or intdiv()
1Check
The RFC behind the PHP 8 change notes that % already threw DivisionByZeroError, and the manual says intdiv() throws it when the second argument is 0.
php -r 'try { intdiv(1, 0); } catch (Error $e) { echo get_class($e), PHP_EOL; }'2Fix
Guard the divisor the same way, or catch the error at the point where you can respond to it. DivisionByZeroError extends Error, not Exception, so catch (Exception) will not catch it.
try {
$pages = intdiv($items, $perPage);
} catch (DivisionByZeroError $e) {
$pages = 0;
}3Verify
Pass a zero page size and confirm the fallback value is used.
php -r 'try { intdiv(5, 0); } catch (DivisionByZeroError $e) { echo "caught", PHP_EOL; }'Confirmed by php.net
Cause 3 of 3
You want IEEE results (INF or NAN), not an exception
1Check
fdiv() is new in PHP 8 and follows IEEE 754: it returns INF, -INF or NAN for a zero divisor and does not throw.
php -r 'var_dump(fdiv(1.0, 0.0), fdiv(0.0, 0.0));'2Fix
Use fdiv() where the old behaviour is really wanted, and test for NAN with is_nan(), since NAN never equals anything, itself included.
$ratio = fdiv($a, $b);
if (is_nan($ratio)) {
$ratio = 0.0;
}3Verify
Run the function with 0.0 as the divisor.
php -r 'var_dump(is_nan(fdiv(0.0, 0.0)));'Confirmed by php.net
How do I stop it coming back?
- Validate numbers from forms, queries and APIs before arithmetic: empty values often become 0.
- Run the test suite on PHP 8.x with error reporting at E_ALL so old warnings show up before release.
- Catch DivisionByZeroError (or Error) in code that handles user-supplied numbers, and log the input.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix