在 PHP 中使用 long int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8647125/
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-26 05:09:34  来源:igfitidea点击:

Using long int in PHP

phpintunsigned-long-long-int

提问by Prashant Singh

I am trying this out, but am unable to store large value

我正在尝试这个,但无法存储大值

$var = rand(100000000000000,999999999999999);
echo $var; // prints a 9 digit value(largest possible)

How to get a desired value ?

如何获得所需的值?

采纳答案by Ned Batchelder

PHP ints are typically 32 bits. Other packages provide higher-precision ints: http://php.net/manual/en/language.types.integer.php

PHP int 通常是 32 位。其他包提供更高精度的整数:http: //php.net/manual/en/language.types.integer.php

回答by DaveRandom

From the 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). 64-bit platforms usually have a maximum value of about 9E18. 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.

...

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.

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

...

如果 PHP 遇到超出整数类型范围的数字,则会将其解释为浮点数。此外,导致数字超出整数类型范围的操作将改为返回浮点数。

BC Mathand GMPare the (only?) way to manipulate this limitation.

BC MathGMP是(唯一的?)操纵这种限制的方法。

回答by Tim Wickstrom

If you need to work with very large numbers I have found success with BC Math. Here is a link to everything you need to know:

如果您需要处理非常大的数字,我已经在 BC Math 中取得了成功。这是您需要了解的所有内容的链接:

http://php.net/manual/en/book.bc.php

http://php.net/manual/en/book.bc.php

回答by jeteon

If you want to generate the number and manipulate as a native type, you can't with most PHP installations (either you have 32 or 64 bit ints and nothing else), as the other answers have already stated. However, if you are just generating a number and want to pass it around a possible trick is to just concatenate strings:

如果您想生成数字并作为本机类型进行操作,则不能使用大多数 PHP 安装(无论是 32 位还是 64 位int,没有其他),正如其他答案已经说明的那样。但是,如果您只是生成一个数字并希望将其传递给一个可能的技巧就是连接字符串:

$var = rand(0,PHP_INT_MAX).str_pad(rand(0, 999999999), 9, 0, STR_PAD_LEFT);
echo $var;

On a platform in which PHP uses a 32 bit integer, this allows you to get a near random integer (as a string) that is bigger than 32 bits ( > 10 decimal places). Of course, there is a bias in this construction which means you won't cover all the numbers with the same probability. The limits of the rand()calls obey normal decimal rules so its simple to adjust the upper bound of the number you want.

在 PHP 使用 32 位整数的平台上,这允许您获得大于 32 位(> 10 位小数)的近似随机整数(作为字符串)。当然,这种结构存在偏差,这意味着您不会以相同的概率覆盖所有数字。rand()调用的限制遵循正常的十进制规则,因此调整所需数字的上限很简单。

If all you're doing is storing/transmitting/showing this value, the string will be just fine. Equality and greater/less than tests will also work. Just don't do any math with it.

如果您所做的只是存储/传输/显示此值,那么字符串就可以了。相等和大于/小于测试也将起作用。只是不要用它做任何数学运算。