Math Functions

PHP 7.0 added intdiv($i1,$i2), which divides two integers and returns an integer result truncated toward zero, unlike the / operator which always returns a float. If $i2 is 0, intdiv() throws a DivisionByZeroError.

In PHP 7, the / operator used to just raise a warning and return INF, -INF, or NAN when dividing by zero. As of PHP 8.0, / throws that same DivisionByZeroError as intdiv() (and so does the % modulo operator). If you genuinely want the old INF / -INF / NAN behavior instead of an exception, use fdiv($f1,$f2), also added in PHP 8.0.

<!DOCTYPE html><html><head></head>
<body><?php

echo intdiv(10, 3) . "<br />";
echo intdiv(-10, 3) . "<br />";
echo (10 / 3) . "<br />";

try {
  echo intdiv(10, 0);
} catch (DivisionByZeroError $e) {
  echo "Caught: " . $e->getMessage() . "<br />";
}

try {
  echo 10 / 0;
} catch (DivisionByZeroError $e) {
  echo "Caught: " . $e->getMessage() . "<br />";
}

echo fdiv(10, 0) . "<br />";

?></body></html>

3
-3
3.3333333333333
Caught: Division by zero
Caught: Division by zero
INF