Use intdiv() when you need to returns the integer quotient of two integers, discarding the remainder in a PHP application. The examples below show the actual operation and its limitations.
What Is intdiv() in PHP?
Returns the integer quotient of two integers, discarding the remainder. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
intdiv(int $num1, int $num2): int
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
echo intdiv(10,3);
Output:
3
Example 2: Different Input or Behavior
<?php
echo intdiv(-10,3);
Output:
-3
Practical Example: Applying the Function
<?php
$items = 23; $itemsPerPage = 5; echo intdiv($items + $itemsPerPage - 1,$itemsPerPage);
Output:
5
Important Behavior and Edge Cases
Dividing by zero throws DivisionByZeroError; PHP_INT_MIN / -1 can overflow.
Comparison With Related Functions
| Approach | Difference |
|---|---|
intdiv() | Returns the integer quotient of two integers, discarding the remainder. |
| Related operation | / returns floating-point division results; intdiv() returns an integer quotient. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Dividing by zero throws DivisionByZeroError; PHP_INT_MIN / -1 can overflow. |
| Confusing this function with a related operation | / returns floating-point division results; intdiv() returns an integer quotient. |
Key Takeaways
- Returns the integer quotient of two integers, discarding the remainder.
- Dividing by zero throws DivisionByZeroError; PHP_INT_MIN / -1 can overflow.
- / returns floating-point division results; intdiv() returns an integer quotient.
Use intdiv() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.