tags:

views:

1509

answers:

5

Ignoring the special libraries that allow you to work with very numbers, what's the largest int you can store in PHP?

A: 

Ah I found it: 232 - 1 (2147483647)

http://au2.php.net/int

Integer overflow

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.

<?php
$large_number =  2147483647;
var_dump($large_number);
// output: int(2147483647)

$large_number =  2147483648;
var_dump($large_number);
// output: float(2147483648)
nickf
This is what it would be on a 32-bit platform. Consider also that many people run servers on a 64-bit platform.
thomasrutter
+1  A: 

The size of PHP ints is platform independent:

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). 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.

PHP 6 adds "longs" (64 bit ints).

cletus
i don't get these downvotes.. this is a perfectly good answer, even with some extra information thrown in. +1 to correct it.
nickf
erm, I think you mean "platform dependant" ;) Fixed it for ya.
thomasrutter
+1  A: 

It depends on your OS, but 2147483647 is the usual value, according to the manual.

Bryson
Specifically, it depends on whether you are running on a 32-bit platform or 64-bit. 32-bit is only "usual" in a world where most people run 32-bit servers. Increasingly this is becoming not the case.
thomasrutter
+6  A: 

From the PHP 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). 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.

karim79
Well, on amd64 linux, which is quite common nowadays, its 9223372036854775807 (2^63-1)
derobert
That's a lot of digits - there's the first reason I can think of to ever choose AMD over Intel when shopping for a dedicated server. :)
karim79
@karim79, I think it's due to the arch being 64-bit, not it being AMD. =]
strager
@karim79, AMD64 is one name of the 64-bit architecture used by both AMD and Intel these days. Other names for it include x64 and Intel 64. As strager says, nothing to do with it being AMD
thomasrutter
A: 

32-bit builds of PHP:

  • Integers can be from -2147483648 to 2147483647

64-bit builds of PHP:

  • Integers can be from -9223372036854775808 to 9223372036854775807

Numbers are inclusive.

Values outside of these ranges are usually represented by floating point values, as are non-integer values within these ranges.

There are exceptions. For example, on a 32-bit build the crc32() function wraps resulting values above 2147483647 to become negative integers, which can be a gotcha when trying to create portable code as a 64-bit build returns them as positive integers.

PHP has no support for "unsigned" integers as such.

thomasrutter