In PHP, the common practice for integer division is to use the division operator /, then combine it with floor(), intval(), or type casting to obtain an integer result. However, this method is not always optimal, especially when performance and readability matter. The intdiv() function was specifically designed to solve these problems. This article dives into how intdiv() works and its advanced techniques, helping you write more efficient and robust PHP code.
intdiv() is a built-in function introduced in PHP 7, used to perform integer division and return an integer result. Its basic syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">intdiv</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$dividend</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$divisor</span></span><span>): </span><span><span class="hljs-keyword">int</span></span><span>
</span></span>
Unlike traditional division, intdiv() directly discards the fractional part instead of rounding. This means its behavior is closer to integer division operations in lower-level languages.
<span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">intdiv</span></span><span>(</span><span><span class="hljs-number">10</span></span><span>, </span><span><span class="hljs-number">3</span></span>); </span><span><span class="hljs-comment">// Output 3</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">intdiv</span></span><span>(-</span><span><span class="hljs-number">10</span></span><span>, </span><span><span class="hljs-number">3</span></span>); </span><span><span class="hljs-comment">// Output -3</span></span><span>
</span></span>
This is more explicit, faster, and avoids unnecessary floating-point operations compared to intval(10 / 3).
Compared to dividing and then converting to an integer, intdiv() performs integer division directly at the low level, avoiding the overhead of floating-point operations. This advantage becomes significant when handling large loops or batch data.
<span><span><span class="hljs-keyword">for</span></span><span> (</span><span><span class="hljs-variable">$i</span></span><span> = </span><span><span class="hljs-number">0</span></span>; </span><span><span class="hljs-variable">$i</span></span><span> < </span><span><span class="hljs-number">1000000</span></span>; </span><span><span class="hljs-variable">$i</span></span>++) {
</span><span><span class="hljs-variable">$a</span></span> = </span><span><span class="hljs-title function_ invoke__">intdiv</span></span>(</span><span><span class="hljs-variable">$i</span></span>, </span><span><span class="hljs-number">3</span></span>); </span><span><span class="hljs-comment">// Much faster than intval($i / 3)</span></span>
}
</span></span>
intdiv() explicitly communicates the intention of “integer division only,” making the code more readable and especially useful in team collaboration.
intdiv() handles negative numbers with truncation toward zero (consistent with C language), unlike floor() or round(), which makes it more consistent and helps prevent hidden logic errors.
When implementing pagination, you often need to calculate the current page or offset:
<span><span><span class="hljs-variable">$page</span></span> = </span><span><span class="hljs-title function_ invoke__">intdiv</span></span>(</span><span><span class="hljs-variable">$offset</span></span>, </span><span><span class="hljs-variable">$limit</span></span>) + </span><span><span class="hljs-number">1</span></span>;
</span></span>
This is more accurate and readable than the traditional (int)($offset / $limit) + 1.
In certain algorithms, such as bitwise simulation, intdiv() can safely replace the right shift operator >>, especially when handling signed values.
<span><span><span class="hljs-comment">// Equivalent to integer division by 2</span></span><span>
</span><span><span class="hljs-variable">$half</span></span> = </span><span><span class="hljs-title function_ invoke__">intdiv</span></span>(</span><span><span class="hljs-variable">$value</span></span>, </span><span><span class="hljs-number">2</span></span>);
</span></span>
When both quotient and remainder are needed, intdiv() and % form a natural pair:
<span><span><span class="hljs-variable">$quotient</span></span> = </span><span><span class="hljs-title function_ invoke__">intdiv</span></span>(</span><span><span class="hljs-variable">$value</span></span>, </span><span><span class="hljs-variable">$chunkSize</span></span>);
</span><span><span class="hljs-variable">$remainder</span></span> = </span><span><span class="hljs-variable">$value</span></span> % </span><span><span class="hljs-variable">$chunkSize</span></span>;
</span></span>
This combination is very common in scenarios like batch uploads, paginated loading, or file chunking.
intdiv() only accepts integer arguments. Passing floats will throw a TypeError.
The divisor cannot be 0, otherwise a DivisionByZeroError will be thrown.
If you are unsure of variable types, it is recommended to perform type checks or explicit conversions before using it.
intdiv() is often overlooked but a very powerful function. It not only improves code efficiency but also enhances semantic clarity. Whether in performance-critical loops or in business logic requiring precise integer division, intdiv() is a reliable tool. Mastering it is an important step toward becoming an advanced PHP developer.