This is an old revision of the document!

PHP RFC: intdiv()


PHP currently lacks a way to do integer division. While (int)(3 / 2), which does a floating-point division then casts to integer, does work most of the time, it does not return the correct result for integers beyond 53 bits as floats cannot represent integers beyond that point without loss of information. Furthermore, doing a floating-point division then casting seems hackish; it is a workaround for a lack of integer division, not a proper way to do it. Integer division can be quite useful in some contexts, for example when splitting a value into rows and columns, or converting seconds to hours and minutes. Having this also makes the language more complete; we have integer remainder (the modulo operator, %) yet not integer division at present.


A new function is added to ext/standard's math functions:

intdiv(int $numerator, int $divisor)

It returns the integer division of $numerator by $divisor. If $divisor is zero, it throws an E_WARNING and returns FALSE, just as the division operator does.

Backward Incompatible Changes

It would conflict with any existing userland functions called intdiv, but that's unlikely.

Proposed PHP Version(s)

Next PHP 5.x (i.e. PHP 5.7).

RFC Impact and Unaffected PHP Functionality

It's just a function, it has no internals or wider impact.

Future Scope

Should we make it shorter, just div()? That done, should we make it an internal function with its own opcode, much like pow()? In that case, why not make it an infix operator, e.g. 3 div 2? Game Maker Language does this, as I noted in References.

Proposed Voting Choices

This is not a language change, so 50%+1 Yes/No to merge into master.

Patches and Tests

A fully-working patch against master with tests has been made. The pull request is here: https://github.com/php/php-src/pull/722


After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature


Somewhat inspired by Python's '' operator, and the equivalent div operator in Game Maker Language (a somewhat obscure C-like partially object-oriented scripting language). ===== Rejected Features ===== None yet.

rfc/intdiv.1405382637.txt.gz · Last modified: 2017/09/22 13:28 (external edit)