PHP intdiv() – Integer Division Function

The article explains PHP's intdiv() function, describing its syntax, parameters, return value, and providing multiple code examples that illustrate integer division results, edge‑case behavior with large integers, division by zero, and the resulting errors.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP intdiv() – Integer Division Function

The intdiv() function returns the integer part of the division of a dividend by a divisor.

Syntax: int intdiv(int $dividend, int $divisor) Parameters: $dividend – the dividend. $divisor – the divisor.

Return value: The integer quotient of $dividend divided by $divisor.

Examples:

<?php
var_dump(intdiv(3, 2));          // int(1)
var_dump(intdiv(-3, 2));         // int(-2)
var_dump(intdiv(3, -2));         // int(-2)
var_dump(intdiv(-3, -2));        // int(1)
var_dump(intdiv(PHP_INT_MAX, PHP_INT_MAX)); // int(1)
var_dump(intdiv(PHP_INT_MIN, PHP_INT_MIN)); // int(1)
var_dump(intdiv(PHP_INT_MIN, -1)); // Fatal error: Uncaught ArithmeticError
var_dump(intdiv(1, 0));          // Fatal error: Uncaught DivisionByZeroError
?>

Output:

int(1)
int(-2)
int(-2)
int(1)
int(1)
int(1)
Fatal error: Uncaught ArithmeticError: Division of PHP_INT_MIN by -1 is not an integer in %s on line 8
Fatal error: Uncaught DivisionByZeroError: Division by zero in %s on line 9

The function throws an ArithmeticError when the division would overflow and a DivisionByZeroError when the divisor is zero.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendError handlingPHP8intdivinteger division
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.