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
num2is0, the function throws aDivisionByZeroErrorexception.- If
num1isPHP_INT_MINandnum2is-1, the function throws anArithmeticErrorexception.
Code
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";}?>