143

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

PHPLover
  • 1
  • 51
  • 158
  • 311
nickf
  • 537,072
  • 198
  • 649
  • 721

8 Answers8

152

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.

64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit.

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 15
    Well, on amd64 linux, which is quite common nowadays, its 9223372036854775807 (2^63-1) – derobert Mar 22 '09 at 07:51
  • 4
    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 Mar 22 '09 at 08:02
  • 41
    @karim79, I think it's due to the arch being 64-bit, not it being AMD. =] – strager Mar 22 '09 at 08:23
  • 9
    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 May 16 '10 at 03:42
  • 4
    My windows XAMPP echo's 2147483647 . I had AMD Athlon X2 – Vova Popov Mar 19 '12 at 20:06
  • 1
    @Vova Popov, Yeah I believe that for some reason if you're running php on windows it's forced into using the 32 bit int even though you have a 64 bit machine. This bug has been a headache on my current project. – RayLoveless Nov 30 '12 at 17:31
  • 1
    @Raymo: Probably has a lot to do with how big your compiler considers a `long` to be. If it's compiled for a 64-bit CPU, but with a compiler that still considers `long` to be 32 bits... – cHao Dec 22 '12 at 23:50
  • 1
    Since PHP 7.0, integers are 64-bit on 64-bit Windows: http://www.php.net/manual/en/language.types.integer.php - have updated the answer to reflect the additions to the manual – Ryall Jul 20 '17 at 13:33
  • @Ryall : What does exactly mean by **"Unsigned Integers"** in the statement **"PHP does not support unsigned integers"** from the above paragraph taken from the PHP manual? Would you please explain me regarding specifically to PHP? Thank You. –  Dec 24 '17 at 03:32
  • @SerialKisser I'm not the original poster, but unsigned integers are integers with an extra bit available to them, because they don't store +/- (the number is always positive). In PHP all integers are signed (can be positive or negative). – Ryall Dec 25 '17 at 16:57
  • @VovaPopov yeah that's why it's AMD64 and not Intel64 (AKA: Itainium) - because it runs 32 bit software too :) – Alec Teal Jan 18 '19 at 13:30
105

32-bit builds of PHP:

  • Integers can be from -2,147,483,648 to 2,147,483,647 (~ ± 2 billion)

64-bit builds of PHP:

  • Integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (~ ± 9 quintillion)

Numbers are inclusive.

Note: some 64-bit builds once used 32-bit integers, particularly older Windows builds of PHP

Values outside of these ranges are represented by floating point values, as are non-integer values within these ranges. The interpreter will automatically determine when this switch to floating point needs to happen based on whether the result value of a calculation can't be represented as an integer.

PHP has no support for "unsigned" integers as such, limiting the maximum value of all integers to the range of a "signed" integer.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • 1
    Interestingly, in 32-bit builds *floats* can retain integer accuracy to higher values than ints - floats can be used for integer values up to 2^53 + 1, significantly higher than the 2^31 - 1 of ints. In 64-bit builds this is reversed because floats are the same precision but ints are now up to 2^63 - 1. – thomasrutter Jul 31 '18 at 00:45
  • > *PHP has no support for "unsigned" integers as such* This means that the maximum value of an `unsigned long` cannot be represented in PHP with an `int`, whether it's 32-bit or 64-bit. Also, a Javascript `number` can represent a maximum integer value of 53 bits, due to that language combining floats and ints in a single `number` type. So when you are designing a schema, it can make sense to take into account that the maximum integer value that can be represented on most systems is signed 53-bit and not (signed or unsigned) 64-bit. – Stijn de Witt Sep 05 '22 at 08:29
  • I think Javascript is relevant here because the `number` limit bleeds into JSON, which also cannot represent integer values larger than 53 bits. – Stijn de Witt Sep 05 '22 at 08:31
  • That 53 bit significance comes from IEEE754 which is also the standard for floating point numbers built right into processors. – thomasrutter Sep 06 '22 at 02:15
  • Yeah, it's just that many languages define specific integer types which are not internally stored as double precision floats. This gives those languages 64-bit integers. Java for example has `long`, which is a signed 64-bit integer. C has `unsigned long long`, which on 64-bit architectures gives you an unsigned 64-bit integer. This means that in Java and C you can create integer values that are much higher that what can be expressed in a `number` in Javascript. This matters when you are creating a JSON API for example. – Stijn de Witt Sep 06 '22 at 09:05
24

The size of PHP ints is platform dependent:

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

Stas Bichenko
  • 13,013
  • 8
  • 45
  • 83
cletus
  • 616,129
  • 168
  • 910
  • 942
  • 2
    i don't get these downvotes.. this is a perfectly good answer, even with some extra information thrown in. +1 to correct it. – nickf Mar 22 '09 at 08:08
  • 3
    This answer originally read "platform *in*dependent" (not dependent). It also (still) claims that 64-bit ints didn't exist before PHP 6. This is false. – thomasrutter Dec 06 '16 at 23:23
  • 1
    PHP 6 never happened though, so it's kind of irrelevant. – BadHorsie Jun 01 '20 at 20:25
  • Whether PHP 6 happened or not, this answer claims that earlier versions of PHP had no 64 bit integers, and even implies that PHP versions since have a data type called a "long" - neither is correct. – thomasrutter Jul 15 '22 at 01:26
18

(a little bit late, but could be useful)

Only trust PHP_INT_MAX and PHP_INT_SIZE, this value vary on your arch (32/64 bits) and your OS...

Any other "guess" or "hint" can be false.

Julien CROUZET
  • 774
  • 9
  • 18
7

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)
Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
2

It subjects to architecture of the server on which PHP runs. For 64-bit,

print PHP_INT_MIN . ", ” . PHP_INT_MAX; yields -9223372036854775808, 9223372036854775807

2

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

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 6
    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 May 16 '10 at 03:47
1

Although PHP_INT_* constants exist for a very long time, the same MIN / MAX values could be found programmatically by left shifting until reaching the negative number:

$x = 1;
while ($x > 0 && $x <<= 1);
echo "MIN: ", $x;
echo PHP_EOL;
echo "MAX: ", ~$x;
revo
  • 47,783
  • 14
  • 74
  • 117