Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

DivisionByZeroError

DivisionByZeroError is an ArithmeticError raised by division or remainder with a zero divisor in PHP 8.4, including intdiv.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

try { intdiv(7, 0); } catch (DivisionByZeroError $e) {}

DivisionByZeroError is an ArithmeticError raised by division or remainder with a zero divisor in PHP 8.4, including intdiv. It belongs to the Error branch of Throwable. The floating-point function fdiv instead follows IEEE arithmetic and can produce infinity or NaN at zero. Check these results with is_infinite or is_nan.

foreach ([fn() => 7 / 0, fn() => 7 % 0, fn() => intdiv(7, 0)] as $f) {
    try { $f(); } catch (DivisionByZeroError $e) { echo get_class($e), PHP_EOL; }
}
echo json_encode([is_infinite(fdiv(7, 0)), is_nan(fdiv(0, 0))]);
DivisionByZeroError
DivisionByZeroError
DivisionByZeroError
[true,true]

Examples

<?php
foreach ([fn() => 7 / 0, fn() => 7 % 0, fn() => intdiv(7, 0)] as $f) {
    try { $f(); } catch (DivisionByZeroError $e) { echo get_class($e), PHP_EOL; }
}
echo json_encode([is_infinite(fdiv(7, 0)), is_nan(fdiv(0, 0))]);

Result

DivisionByZeroError
DivisionByZeroError
DivisionByZeroError
[true,true]

Sources