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.
PHP Integers An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems. A value greater (or lower) than this, will be stored as float, because it exceeds the limit of an integer.
There are a number of ways to "convert" an integer to a string in PHP. The traditional computer science way would be to cast the variable as a string: $int = 5; $int_as_string = (string) $int; echo $int . ' is a '.
PHP doesn't by default make a variable integer or string, if you want to set default value, then simply write $myvariable = 0; and this will assign and make variable an integer type.
Now array[0] and array[1] is the first two digits. array[array. length-1] and array[array. length] are the last two.
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
That number is too big to fit into a 32 bit integer so PHP is storing it in a float.
See the integer overflow section in the php manual.
On the off chance that you need really big integers, look into GMP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With