intdiv()
Today, we will discuss the intdiv() function in PHP. This function is used to perform integer division and return the quotient.
The intdiv() function in PHP performs integer division: it divides one integer by another and returns the whole-number quotient, discarding any remainder. It was introduced in PHP 7.0 to give you a clear, explicit way to express "how many times does B fit into A" without resorting to the / operator and a manual cast.
This page explains what intdiv() returns, how it differs from the / and % operators, how it treats negative numbers and floats, and how to handle the errors it can throw.
Syntax
intdiv(int $num1, int $num2): int$num1— the dividend (the number being divided).$num2— the divisor (the number you divide by).- Returns the quotient
$num1 / $num2as anint, with the fractional part dropped.
A Basic Example
10 / 3 is 3.333…, so intdiv() keeps the whole part, 3, and throws away the 0.333….
intdiv() vs. the / and % Operators
The regular division operator / always produces a float when the numbers do not divide evenly, while intdiv() always produces an int. The modulo operator % gives you the remainder that intdiv() discards. Together, intdiv() and % recover the original number:
<?php
echo 10 / 3; // 3.3333333333333 (float)
echo "\n";
echo intdiv(10, 3); // 3 (int quotient)
echo "\n";
echo 10 % 3; // 1 (remainder)
echo "\n";
// quotient * divisor + remainder == dividend
echo intdiv(10, 3) * 3 + (10 % 3); // 10
?>Use intdiv() when you genuinely want an integer result, such as splitting items into pages, converting seconds into minutes, or distributing a count into fixed-size buckets.
Truncation Toward Zero
intdiv() truncates toward zero, not toward negative infinity. This matters for negative operands and is a common source of bugs when people assume it rounds down:
<?php
echo intdiv(7, 2); // 3
echo "\n";
echo intdiv(-7, 2); // -3 (not -4)
echo "\n";
echo intdiv(7, -2); // -3
echo "\n";
echo intdiv(-7, -2); // 3
?>-7 / 2 is -3.5; truncating toward zero gives -3. (If you specifically want floor behaviour, use floor(-7 / 2), which returns -4.)
Handling Errors
intdiv() throws an exception in two cases, both of which you should guard against:
- Division by zero throws a
DivisionByZeroError. intdiv(PHP_INT_MIN, -1)throws anArithmeticError, because the mathematically correct result is larger than the maximum value an integer can hold.
<?php
function safeIntdiv($a, $b) {
try {
return intdiv($a, $b);
} catch (DivisionByZeroError $e) {
return "Cannot divide by zero";
} catch (ArithmeticError $e) {
return "Result out of integer range";
}
}
echo safeIntdiv(20, 4); // 5
echo "\n";
echo safeIntdiv(20, 0); // Cannot divide by zero
?>Note:
intdiv()expects integer arguments. A float such as7.9is coerced to an integer (its fractional part dropped) before the division runs, sointdiv(7.9, 2)behaves likeintdiv(7, 2). For division that keeps the fractional part of floats, use thefmod()function or the/operator instead.
Conclusion
The intdiv() function provides a reliable, explicit way to perform integer division in PHP. Remember that it truncates toward zero, that the discarded remainder is available through %, and that it can throw DivisionByZeroError and ArithmeticError. For more on numeric operations, see PHP Math, PHP Operators, and PHP Numbers.