Using long int in PHP

Prashant Singh picture Prashant Singh · Dec 27, 2011 · Viewed 64.6k times · Source

I am trying this out, but am unable to store large value

$var = rand(100000000000000,999999999999999);
echo $var; // prints a 9 digit value(largest possible)

How to get a desired value ?

Answer

DaveRandom picture DaveRandom · Dec 27, 2011

From the manual:

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

...

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

BC Math and GMP are the (only?) way to manipulate this limitation.