What is the intdiv() function in PHP?

The intdiv() function in PHP divides two integers and returns a quotient.

Syntax

intdiv(int $num1, int $num2): int

Parameters

The function requires two parameters:

  • num1, which should be an integer.
  • num2, which should be another integer (it shouldn’t be zero).

Return value

The function returns the quotient of num1/num2.

Note: The function will throw an exception in two cases:

  • If num2 is 0, the function throws a DivisionByZeroError exception.
  • If num1 is PHP_INT_MIN and num2 is -1, the function throws an ArithmeticError exception.

Code

In this example, we will see how to use the intdiv() function and how to handle exceptions:

<?php
echo intdiv(5, 2) . "\n";
try {
echo intdiv(1, 0);
} catch(DivisionByZeroError $e){
echo "Cant divide by 0\n";
}
try {
echo intdiv(PHP_INT_MIN, -1);
} catch(ArithmeticError $e){
echo "Cant divide " . ((string) PHP_INT_MIN). " by -1";
}
?>

Free Resources