PHP intdiv() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The intdiv() function divides two whole numbers and returns only the whole number part of the answer.

It is a clean way to do integer division without using floor() or casting.

Syntax

Call intdiv() with the values you need. Start with this line.

PHP
You pass a dividend and a divisor to intdiv() and it returns the quotient as an integer.
CallWhat it does
intdiv(10, 3)Returns 3.
intdiv(20, 5)Returns 4.
intdiv(7, 2)Returns 3, because the .5 part is dropped.

Use the % operator when you want the remainder of the same division.

Library declarations
PHP
intdiv(int $num1, int $num2): int

Parameters

ParameterDescription
num1Required. The number to be divided (the dividend).
num2Required. The number to divide by (the divisor). It cannot be 0.

Return Value

Return ValueDescription
intThe integer result of num1 / num2, rounded toward zero.

PHP Examples

Example 1 — Whole number division

Divide a few numbers and keep only the whole part.

PHP
<?php
echo intdiv(10, 3) . PHP_EOL;
echo intdiv(20, 5) . PHP_EOL;
echo intdiv(7, 2) . PHP_EOL;

How It Works

  1. 10 divided by 3 is 3.33, so intdiv() returns 3.
  2. 20 divided by 5 is exactly 4.
  3. 7 divided by 2 is 3.5, and the .5 is dropped, giving 3.
  4. The return type is always int.

Example 2 — Negative numbers and division by zero

See how the result is truncated and how to handle zero safely.

PHP
<?php
echo intdiv(-7, 2) . PHP_EOL;

try {
    echo intdiv(1, 0) . PHP_EOL;
} catch (DivisionByZeroError $e) {
    echo "Error: " . $e->getMessage() . PHP_EOL;
}

How It Works

  1. -7 divided by 2 is -3.5, and intdiv() truncates toward zero, so the result is -3.
  2. floor(-3.5) would give -4, so intdiv() and floor() are not the same for negatives.
  3. intdiv(1, 0) throws DivisionByZeroError.
  4. The try and catch block catches the error and prints the message.

Example 3 — Convert seconds to hours, minutes and seconds

Use intdiv() together with the % operator to split a time value.

PHP
<?php
$totalSeconds = 3725;

$hours = intdiv($totalSeconds, 3600);
$remaining = $totalSeconds % 3600;
$minutes = intdiv($remaining, 60);
$seconds = $remaining % 60;

echo $hours . "h " . $minutes . "m " . $seconds . "s" . PHP_EOL;

How It Works

  1. intdiv(3725, 3600) gives 1 full hour.
  2. 3725 % 3600 leaves 125 seconds.
  3. intdiv(125, 60) gives 2 full minutes, and 125 % 60 leaves 5 seconds.
  4. The pieces are joined into the text 1h 2m 5s.

Frequently Asked Questions

It divides two integers and returns the integer part of the result, dropping any decimals.
The / operator returns a float when the result is not whole, like 10 / 4 = 2.5. intdiv(10, 4) returns 2.
intdiv() throws DivisionByZeroError. Wrap the call in try and catch or check the divisor first.
It truncates toward zero. For positive numbers that is the same as rounding down, but intdiv(-7, 2) is -3, not -4.
Use the % operator, like 10 % 3, which returns 1.

Next: is_finite()

The is_finite() function checks whether a number is a normal, finite number.

is_finite() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful