PHP 中 int 的最大值是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/670662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:25:58  来源:igfitidea点击:

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

phpinteger

提问by nickf

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 中存储的最大 int 值是多少?

回答by karim79

From the PHP manual:

PHP 手册

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.

整数的大小取决于平台,但通常的值(即 32 位有符号)约为 20 亿的最大值。PHP 不支持无符号整数。自 PHP 4.4.0 和 PHP 5.0.5 起,整数大小可以使用常量 PHP_INT_SIZE 确定,最大值使用常量 PHP_INT_MAX 确定。

64 位平台的最大值通常约为 9E18,但在 PHP 7 之前的 Windows 上,它始终为 32 位。

回答by thomasrutter

32-bit builds of PHP:

32 位 PHP 版本:

  • Integers can be from -2,147,483,648to 2,147,483,647(~ ± 2 billion)
  • 整数可以从-2,147,483,6482,147,483,647(~±20 亿)

64-bit builds of PHP:

64 位 PHP 版本:

  • Integers can be from -9,223,372,036,854,775,808to 9,223,372,036,854,775,807(~ ± 9 quintillion)
  • 整数可以从-9,223,372,036,854,775,8089,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

注意:一些 64 位版本曾经使用过 32 位整数,尤其是旧的 Windows 版本的 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.

PHP 不支持“无符号”整数,因此将所有整数的最大值限制在“有符号”整数的范围内。

回答by cletus

The size of PHP ints is platform dependent:

PHP int 的大小取决于平台

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.

整数的大小取决于平台,但通常的值(即 32 位有符号)约为 20 亿的最大值。PHP 不支持无符号整数。自 PHP 4.4.0 和 PHP 5.0.5 起,整数大小可以使用常量 PHP_INT_SIZE 确定,最大值使用常量 PHP_INT_MAX 确定。

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

PHP 6 添加了“longs”(64 位整数)。

回答by Julien CROUZET

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

只信任PHP_INT_MAX 和 PHP_INT_SIZE,这个值因你的架构(32/64 位)和你的操作系统而异......

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

任何其他“猜测”或“暗示”都可能是错误的。

回答by nickf

Ah I found it: 232- 1 (2147483647)

啊我找到了:2 32- 1 (2147483647)

http://au2.php.net/int

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 遇到超出整数类型范围的数字,则会将其解释为浮点数。此外,导致数字超出整数类型范围的操作将改为返回浮点数。

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

$large_number =  2147483648;
var_dump($large_number);
// output: float(2147483648)

回答by nickf

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

这取决于您的操作系统,但根据手册,2147483647 是通常的值。

回答by revo

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:

尽管PHP_INT_*常量存在很长时间,但可以通过左移直到达到负数以编程方式找到相同的 MIN / MAX 值:

$x = 1;
while ($x > 0 && $x <<= 1);
echo "MIN: ", $x;
echo PHP_EOL;
echo "MAX: ", ~$x;

回答by snr

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

它取决于运行 PHP 的服务器的架构。对于 64 位,

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

print PHP_INT_MIN . ", ” . PHP_INT_MAX;产量 -9223372036854775808, 9223372036854775807