What it is and why to use it
Packing 17 items into boxes that hold five items raises two questions: how many full boxes fit, and how many items remain? intdiv() answers the quotient question with an integer result. The remainder operator % can answer the second. This is useful for grouping, pagination calculations and other deliberately whole-number operations.
How it works
intdiv divides two integers and truncates the result toward zero. For positive values this agrees with taking the floor, but -7 divided by 3 becomes -2 rather than -3. A zero divisor throws DivisionByZeroError. Dividing PHP_INT_MIN by -1 throws ArithmeticError because the positive result cannot be represented as a PHP integer on that platform.
Common mistakes and limitations
Do not use integer division when fractional results are meaningful. Do not describe negative quotients as always rounded down. Validate divisors derived from input and consider integer range limits when multiplying or accumulating large values.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$whole = intdiv($total, $groupSize);Examples
Count full boxes and leftovers
Separate whole groups from the remaining items.
<?php
declare(strict_types=1);
$items = 17;
$capacity = 5;
echo intdiv($items, $capacity), ' full boxes', PHP_EOL;
echo $items % $capacity, ' items left', PHP_EOL;
- Three groups of five fit into seventeen; intdiv discards the fractional part of 3.4.
- The remainder expression finds the two items not included in those complete groups.
- This is useful for a packing estimate with positive integers. An application would validate that capacity is positive.
- Expected output: 3 full boxes 2 items left
Recognise truncation and handle division by zero
Show a negative quotient and a controlled error path.
<?php
declare(strict_types=1);
echo intdiv(-7, 3), PHP_EOL;
try {
echo intdiv(10, 0), PHP_EOL;
} catch (DivisionByZeroError $error) {
echo 'Choose a nonzero divisor', PHP_EOL;
}
- The first quotient is truncated toward zero, producing -2.
- The second operation throws before echo can print a number.
- The catch handles the specific error and prints a controlled message. This demonstrates error handling rather than recommending exceptions as a substitute for validating a known zero divisor.
- Expected output: -2 Choose a nonzero divisor
Best practices
- Use integer division only when truncation is the intended rule.
- Check that a divisor is nonzero before accepting it from input.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual