PHP DivisionByZeroError

The DivisionByZeroError class is a subclass of ArithmeticError class. This type of error occurs when division operations involve a denominator value of zero. This can also occur when a modulo operator (%) has 0 as the second operand, and the intdiv() function has 0 as its second argument.

Modulo Division by Zero

The modulo operator (%) throws a DivisionByZeroError when the divisor is zero ?

<?php
try {
    $a = 10;
    $b = 0;
    $result = $a % $b;
    echo $result;
}
catch (DivisionByZeroError $e) {
    echo $e->getMessage();
}
?>
Modulo by zero

Integer Division by Zero

The intdiv() function also raises DivisionByZeroError when the divisor is zero ?

<?php
try {
    $a = 10;
    $b = 0;
    $result = intdiv($a, $b);
    echo $result;
}
catch (DivisionByZeroError $e) {
    echo $e->getMessage();
}
?>
Division by zero

Float Division by Zero

The division operator (/) with 0 as denominator does not raise an error but produces a warning and returns the PHP constant INF (infinity) ?

<?php
try {
    $a = 10;
    $b = 0;
    $result = $a / $b;
    echo $result;
}
catch (DivisionByZeroError $e) {
    echo $e->getMessage();
}
?>
INF
Updated on: 2026-03-15T09:23:28+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements