intdiv

Divides one integer by another and returns the integer quotient, truncated toward zero.

Signature
intdiv(int $num1, int $num2): int
  • Added in PHP 7.0

Parameters

ParameterTypeDescription
$num1intNumber to be divided.
$num2intNumber which divides num1.

Return value

intThe integer quotient of the division of num1 by num2, truncated toward zero.

Errors

Throws DivisionByZeroError when num2 is 0, and ArithmeticError when num1 is PHP_INT_MIN and num2 is -1, because that result cannot be represented as an int.

intdiv() divides one integer by another and gives back an integer, discarding the fractional part. It is the only division in PHP guaranteed to return an int.

Why not just use /

The / operator returns a float whenever the division is not exact, so (int) (10 / 3) makes a round trip through a double before you get your integer back. For operands near PHP_INT_MAX that round trip loses precision, because a double cannot represent every 64-bit integer. intdiv() stays in integer arithmetic the whole way and cannot drift.

Truncation, not floor

The result is truncated toward zero, matching C and most other languages, which is not the same as floor division. For positive operands the two agree; for negative ones they differ by one. If you need Python-style floor behaviour, use (int) floor($a / $b) and accept the float round trip.

Both arguments must be integers. A float with a fractional part triggers a deprecation notice about lost precision, and under declare(strict_types=1) any float argument is a TypeError. Pair intdiv() with % when you want both the quotient and the remainder.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Splitting a total record count into fixed-size pages without float rounding error.
  • Converting a byte count into whole kilobytes or a duration in seconds into whole minutes.
  • Bucketing an index into fixed-width groups, such as intdiv($i, 3) for a three-column grid.
  • Halving a very large integer ID without it ever passing through a float.

Frequently asked questions

What is the difference between intdiv and the / operator?

The / operator returns a float whenever the division is not exact, while intdiv() always returns an int with the remainder discarded. intdiv(7, 2) is int(3); 7 / 2 is float(3.5).

Why does intdiv(-7, 2) return -3 instead of -4?

intdiv() truncates toward zero rather than rounding down, so a negative result loses its fraction in the direction of zero. For floor division use (int) floor(-7 / 2), which gives -4.

What happens if you divide by zero with intdiv?

It throws DivisionByZeroError, which you can catch. It extends ArithmeticError, so catching ArithmeticError also covers the PHP_INT_MIN divided by -1 overflow case.

More examples

intdiv() next to / and %

The / operator produces float(3.5) because the division is not exact. intdiv() gives the quotient int(3) and % gives the remainder int(1). The last line shows the identity that holds for every pair: quotient times divisor plus remainder equals the original numerator, exactly, with no float involved.

Example · php
<?php
$a = 7;
$b = 2;
var_dump($a / $b);
var_dump(intdiv($a, $b));
var_dump($a % $b);
var_dump(intdiv($a, $b) * $b + $a % $b);

The two error cases

Negating PHP_INT_MIN would need one more value than the int range holds, so intdiv() refuses with an ArithmeticError. The / operator does not refuse — it silently promotes to a float and hands back an approximation. DivisionByZeroError extends ArithmeticError, so catching ArithmeticError covers both failure modes.

Example · php
<?php
try {
    intdiv(PHP_INT_MIN, -1);
} catch (ArithmeticError $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

var_dump(PHP_INT_MIN / -1);
var_dump(intdiv(PHP_INT_MAX, PHP_INT_MAX));

Pagination without floats

47 records at 10 per page gives 4 complete pages plus a partial one, so 5 pages in total with 7 rows on the last. Doing this with ceil($total / $perPage) works too, but goes through a float; the integer version is exact for any count.

Example · php
<?php
$total   = 47;
$perPage = 10;

$fullPages  = intdiv($total, $perPage);
$totalPages = $fullPages + ($total % $perPage > 0 ? 1 : 0);

echo "full pages:  $fullPages\n";
echo "total pages: $totalPages\n";
echo "last page holds: ", $total % $perPage, " rows\n";

Discussion

  • Be the first to comment on this lesson.