gremo
gremo

Reputation: 48899

Handling large numbers in PHP

After a couple of minutes i've realize the bug that i was having: the magic 2147483647 number, that upper limit for integer type on PHP/32. I need to manage biggers number in my function:

public function albumExists($name) // e.g. 104112826372452
{
   $albums = $this->getAlbums();

   // If $name is int, search the key in $albums
   if(is_int($name) && ($found = array_key_exists($id = intval($name), $albums)))
      return ($found ? $id : false);

   // Start looking for $name as string
   foreach($album as $id => $a) if ($a->name == $name) return intval($id);

   return false; // Found nothing
}

in order to give the ability to search both by id and name. But intval() will always return the upper limit. How can handle quite big numbers like, say, 104112826372452? Ideas?

EDIT: usage example:

$album = $fb->createAlbum('Test Album'); // Will return album id
// The use albumExists to check if id exists
$photo1 = $fb->uploadPhoto('mypic1.png', null, $album); 
$photo2 = $fb->uploadPhoto('mypic2.png', null, 'Test Album'); // find or create

Upvotes: 0

Views: 1978

Answers (5)

mario
mario

Reputation: 145482

As workaround you can use the gmp or bcmath functions for that.

It's not quite clear why you insist on casting to PHP integers. Just leave your database numbering as strings, when don't need to calculate with them. Not everything that looks like a number needs to be represented as number.

I guess your real problem is the differentation with is_int(). Just use is_numeric() in its place, which works with arbitrary-length numeric strings and does not depend on integer-casted values.

Upvotes: 2

thinice
thinice

Reputation: 710

If you're converting to an int for sanity purposes (so it appears), perhaps you could just adjust it to evaluate it purely on it's numeric basis instead of int datatype:

if(ctype_digit($name) && ($found = array_key_exists($id = $name, $albums)))
      return ($found ? $id : false);
//etc

Actually, should this work too?

if(ctype_digit($name) && ($found = array_key_exists($name, $albums)))
      return ($found ? $name: false);
//etc

Upvotes: 2

user336242
user336242

Reputation:

One option is to run PHP on a 64bit OS as the in size is determined by the underlying operating system. This is obviously dependent if you can get access to 64bit hardware, one thing to note is that this will be faster than using gmp/bcmath but unless pure speed is your aim it probably won't be an issue for you

Upvotes: 0

Sean H Jenkins
Sean H Jenkins

Reputation: 1780

Unfornuately PHP int type can only go upto 2147483647 but PHP float can hold integers upto 10000000000000

Check out php.net http://php.net/manual/en/language.types.integer.php

UPDATE

PHP.net says that a float can accurately hold an integer upto 10000000000000. Im not sure if float has an upper limit though.

Upvotes: 0

deceze
deceze

Reputation: 522016

An int has an upper limit, and bigger numbers will be represented as floats, which are imprecise and therefore a bad idea to use in this situation. Use a string to store such numbers and the BC Math extension if you need to do calculations on it.

Upvotes: 0

Related Questions