Search code examples
phpinteger

Why does PHP transform 15+ digit long integer on output


Why does echo 100000000000000; output 1.0E+14 and not 100000000000000?

This kind of transformation of integers on output happens only for integers that are 15 digits long and longer.


Solution

  • PHP will convert an integer to float if it is bigger than PHP_INT_MAX. For 32-bit systems this is 2147483647.

    The answer to the questions is related to the string representation of floats in PHP.

    If the exponent in the scientific notation is bigger than 13 or smaller than -4, PHP will use scientific representation when printing floats.

    Examples:

    echo 0.0001; // 0.0001;
    
    echo 0.00001; // 1.0E-5
    
    // 14 digits
    echo 10000000000000; // 10000000000000
    
    // 15 digits
    echo 100000000000000; // 1.0E+14
    

    See float vs. double precision