What's the maximum value for an int in PHP?

PhpInteger

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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.

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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)

Solution 6 - Php

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

Solution 7 - Php

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

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

Solution 8 - Php

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickfView Question on Stackoverflow
Solution 1 - Phpkarim79View Answer on Stackoverflow
Solution 2 - PhpthomasrutterView Answer on Stackoverflow
Solution 3 - PhpcletusView Answer on Stackoverflow
Solution 4 - PhpJulien CROUZETView Answer on Stackoverflow
Solution 5 - PhpnickfView Answer on Stackoverflow
Solution 6 - Phpuser57365View Answer on Stackoverflow
Solution 7 - PhpsnrView Answer on Stackoverflow
Solution 8 - PhprevoView Answer on Stackoverflow