PHP DivisionByZeroError — Division or modulo by zero
DivisionByZeroError: division by zero
Verified against PHP 8.3 documentation, PHP ArithmeticError class docs, IEEE 754 specification · Updated June 2026
> quick_fix
Check that the divisor is not zero before the division or modulo operation. For intdiv() and %, PHP throws DivisionByZeroError. For the / operator with floats, PHP returns INF or NAN instead of throwing.
<?php
// Guard before integer division
function safeDiv(int $numerator, int $denominator): ?int
{
if ($denominator === 0) {
return null; // or throw a domain exception
}
return intdiv($numerator, $denominator);
}
// Guard before modulo
if ($divisor !== 0) {
$remainder = $value % $divisor;
}
// Float division returns INF, not an exception
$result = 10 / 0; // PHP warning + returns INF (not a DivisionByZeroError)
$result2 = intdiv(10, 0); // throws DivisionByZeroError
$result3 = 10 % 0; // throws DivisionByZeroErrorWhat causes this error
PHP throws DivisionByZeroError (a subclass of ArithmeticError, itself a subclass of Error) when you use intdiv() with a zero divisor or use the modulo operator (%) with a zero divisor. The float division operator (/) with zero does NOT throw — it returns INF, -INF, or NAN and emits a warning, consistent with IEEE 754 float arithmetic.
How to fix it
- 01
step 1
Identify which operator triggers the error
Check whether you are using `intdiv()`, `%`, or `/`. Only intdiv() and % throw DivisionByZeroError. The float `/` operator returns INF/NAN with a warning. The stack trace will point to the exact line.
- 02
step 2
Guard the divisor before the operation
Add: `if ($divisor === 0) { return null; // or throw new InvalidArgumentException }` before the division or modulo. Use strict comparison (`===`) not loose (`==`) to avoid '0' string edge cases.
- 03
step 3
Trace where the divisor value originates
The divisor is usually computed or comes from external data (user input, database row, formula result). Find its origin and ensure the zero case is handled at the source — with a validation message or a domain-meaningful default.
- 04
step 4
For percentage calculations, handle the empty-set case
A common trigger: `$percent = ($count / $total) * 100` when $total is 0 (no records). The correct business response is usually 0% or N/A, not an exception. Return 0 explicitly when $total === 0.
- 05
step 5
Check for ArithmeticError if also using intdiv directly
DivisionByZeroError extends ArithmeticError. You can catch it: `try { $r = intdiv($a, $b); } catch (\DivisionByZeroError $e) { $r = 0; }`. However, prefer the guard check so the catch is not needed for normal flow.
How to verify the fix
- Pass zero as the divisor — the code should return the default/fallback instead of throwing
- Run unit tests with both zero and non-zero divisors
- Verify no division warning appears in PHP logs for float division
Why DivisionByZeroError happens at the runtime level
PHP's integer division function intdiv() and the modulo operator % perform integer arithmetic which has no representation for infinity. The PHP engine throws DivisionByZeroError (an instance of Error, not Exception) when the divisor is zero to prevent undefined behaviour. Float division uses the / operator with IEEE 754 semantics where division by zero yields ±INF, which is a representable float value. This split behaviour — exception for integers, INF for floats — reflects the underlying arithmetic standards rather than a PHP design choice.
Common debug mistakes for DivisionByZeroError
- Using intdiv() for percentage calculations without checking if the total is zero
- Computing remainder with % in pagination code when total items is zero
- Confusing that float `10 / 0` does not throw while `intdiv(10, 0)` does
- Checking `if ($divisor == 0)` with loose comparison — the string '0' would pass but `$divisor === 0` would not catch it
When DivisionByZeroError signals a deeper problem
DivisionByZeroError in production almost always means a denominator was assumed to always be positive (record count, total users, sum of values) but an edge case — empty table, no matching records, all-zero input — was not considered. The fix is domain-specific: for analytics ratios and percentages, define what the answer means when the denominator is zero (0%, N/A, or a special value) and encode that definition explicitly rather than letting the math operation fail.
Editor's take
Division by zero in PHP has different behavior depending on your PHP version and the type of division. In PHP 8.0+, integer division by zero throws a `DivisionByZeroError` (which is catchable), while float division by zero returns `INF` or `NAN` with a warning in PHP 7.x but throws in strict mode in 8.x. This inconsistency across versions is a common migration trap. The most dangerous production scenario isn't `$x / 0` — it's `$total / $count` where `$count` comes from a database query that returns zero rows, or `$revenue / $users` in an analytics dashboard during the first day when there are no users yet. These are logic-level bugs where the zero divisor is perfectly valid data but your code doesn't handle the edge case. The defensive pattern in PHP is straightforward: `$result = $divisor !== 0 ? $dividend / $divisor : 0;` or using the `fdiv()` function introduced in PHP 8.0 which returns INF instead of throwing. In financial calculations, silently returning 0 or INF when dividing by zero can produce incorrect reports that go unnoticed for weeks — it's better to throw a domain-specific exception that forces the calling code to handle the edge case explicitly. The modulo operator `%` has the same behavior: `$a % 0` also throws DivisionByZeroError in PHP 8. Always validate your divisor before arithmetic operations, especially when the value comes from user input or database aggregations.
By Bikram Nath · Curator · Updated June 2026
Frequently asked questions
Why does 10 / 0 return INF in PHP but intdiv(10, 0) throws?
PHP's / operator for floats follows IEEE 754, which defines division by zero as positive or negative infinity (not an error). Integer division has no such standard — there is no 'integer infinity' — so PHP throws DivisionByZeroError instead. This inconsistency surprises many developers.
Was DivisionByZeroError always an Error in PHP?
In PHP 5 and early PHP 7, division by zero for the / operator triggered a warning and returned false. PHP 7.0 introduced the Error exception hierarchy and DivisionByZeroError. PHP 8 made the behavior more consistent. If you are on PHP 5 and see division warnings, upgrade — the behaviour is significantly improved in PHP 8.
What happens when I do 0 / 0 in PHP?
With the / float operator: PHP emits a DivisionByZeroError warning and returns NAN (Not a Number). With intdiv(0, 0): throws DivisionByZeroError. With 0 % 0: throws DivisionByZeroError. NAN comparisons are tricky — `NAN == NAN` is false, use `is_nan($result)` to check.