DivisionByZeroError
PHPERRORCommonMath
Integer divided or modulo by zero
Quick Answer
Guard divisors before integer division; use fdiv() for floating-point division that returns INF/NAN instead of throwing.
What this means
Thrown when an integer is divided by zero using / or % operators. Note: fdiv() returns INF or NAN instead of throwing.
Why it happens
- 1$a % 0 or intdiv($a, 0)
- 2Divisor derived from user input without validation
Fix
Guard divisor
Guard divisor
$result = $divisor !== 0 ? intdiv($a, $divisor) : 0; // Float-safe alternative $result = fdiv($a, $divisor); // returns INF or NAN, never throws
Why this works
fdiv() follows IEEE 754 — never throws, returns INF/NAN for division by zero.
Code examples
Triggerphp
$x = 10 % 0; // DivisionByZeroError
Safe integer dividephp
function safeDivide(int $a, int $b): int {
return $b !== 0 ? intdiv($a, $b) : 0;
}fdiv for floatsphp
$r = fdiv(10.0, 0.0); // INF — no exception
Same error in other languages
Sources
Official documentation ↗
PHP Manual
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev