The intdiv()
function in PHP divides two integers and returns a quotient.
intdiv(int $num1, int $num2): int
The function requires two parameters:
num1
, which should be an integer.num2
, which should be another integer (it shouldn’t be zero).The function returns the quotient of num1/num2
.
Note: The function will throw an exception in two cases:
- If
num2
is0
, the function throws aDivisionByZeroError
exception.- If
num1
isPHP_INT_MIN
andnum2
is-1
, the function throws anArithmeticError
exception.
In this example, we will see how to use the intdiv()
function and how to handle exceptions:
<?phpecho 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";}?>