PHP Outputting large integers


Example

On 32-bits systems, integers larger than PHP_INT_MAX are automatically converted to float. Outputting these as integer values (i.e. non-scientific notation) can be done with printf, using the float representation, as illustrated below:

foreach ([1, 2, 3, 4, 5, 6, 9, 12] as $p) {
    $i = pow(1024, $p);
    printf("pow(1024, %d) > (%7s) %20s %38.0F", $p, gettype($i), $i, $i);
    echo "  ", $i, "\n";
}
// outputs:
pow(1024,  1)  integer                 1024                                   1024  1024
pow(1024,  2)  integer              1048576                                1048576  1048576
pow(1024,  3)  integer           1073741824                             1073741824  1073741824
pow(1024,  4)   double        1099511627776                          1099511627776  1099511627776
pow(1024,  5)   double  1.1258999068426E+15                       1125899906842624  1.1258999068426E+15
pow(1024,  6)   double  1.1529215046068E+18                    1152921504606846976  1.1529215046068E+18
pow(1024,  9)   double  1.2379400392854E+27           1237940039285380274899124224  1.2379400392854E+27
pow(1024, 12)   double  1.3292279957849E+36  1329227995784915872903807060280344576  1.3292279957849E+36

Note: watch out for float precision, which is not infinite!

While this looks nice, in this contrived example the numbers can all be represented as a binary number since they are all powers of 1024 (and thus 2). See for example:

$n = pow(10, 27);
printf("%s %.0F\n", $n, $n);
// 1.0E+27 1000000000000000013287555072