PHP 中如何获得 64 位整数?

发布于 2024-07-19 11:29:59 字数 68 浏览 2 评论 0原文

PHP 中如何获得 64 位整数?

看起来它不是由配置文件决定的,而可能是编译时选项,并且取决于平台。

How can I have a 64-bit integer in PHP?

It seems like it is not by a config file, but rather it might be a compile-time option, and it depends on the platform.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

治碍 2024-07-26 11:30:00

本机 64 位整数需要 64 位硬件和 64 位版本的 PHP。

在 32 位硬件上:

$ php -r 'echo PHP_INT_MAX;'
2147483647

在 64 位硬件上:

$ php -r 'echo PHP_INT_MAX;'
9223372036854775807

Native 64-bit integers require 64-bit hardware AND the 64-bit version of PHP.

On 32-bit hardware:

$ php -r 'echo PHP_INT_MAX;'
2147483647

On 64-bit hardware:

$ php -r 'echo PHP_INT_MAX;'
9223372036854775807
菩提树下叶撕阳。 2024-07-26 11:30:00

更新:现在可以了(在 AMD 四核、Windows 8.1 上测试)。

请注意,Windows 上的 PHP 根本不支持 64 位整数,即使硬件和 PHP 都是 64 位。 有关详细信息,请参阅此链接

在 Windows x86_64 上,PHP_INT_MAX 为 2147483647。这是因为在底层 C 代码中,long 是 32 位。

但是,x86_64 上的 Linux 使用 64 位长,因此 PHP_INT_MAX 将是
9223372036854775807。

UPDATE: It does now (tested on AMD quad core, Windows 8.1).

Note that PHP on Windows does not support 64-bit integers at all, even if both the hardware and PHP are 64-bit. See this link for details:

On Windows x86_64, PHP_INT_MAX is 2147483647. This is because in the underlying C code, a long is 32 bit.

However, Linux on x86_64 uses a 64-bit long, so PHP_INT_MAX is going to be
9223372036854775807.

终遇你 2024-07-26 11:30:00

也许您可以使用 GMPBCMath 扩展。

Maybe you could use either the GMP or BCMath extension.

长不大的小祸害 2024-07-26 11:30:00

PHP int 大小与平台相关。 有一个名为 unpack() 的函数,它本质上允许将不同类型的数据从二进制字符串转换为 PHP 变量。 存储为 64 位的唯一方法似乎是将其存储为字符串。

我在以下位置找到了以下代码:
http://www.mysqlperformanceblog.com/2007/03/27/integers-in-php-running-with-scissors-and-portability/" mysqlperformanceblog.com/2007/03/27/integers-in-php-running-with-scissors-and-portability/

/// portably build 64bit id from 32bit hi and lo parts
function _Make64 ( $hi, $lo )
{

        // on x64, we can just use int
        if ( ((int)4294967296)!=0 )
            return (((int)$hi)<<32) + ((int)$lo);

        // workaround signed/unsigned braindamage on x32
        $hi = sprintf ( "%u", $hi );
        $lo = sprintf ( "%u", $lo );

        // use GMP or bcmath if possible
        if ( function_exists("gmp_mul") )
            return gmp_strval ( gmp_add ( gmp_mul ( $hi, "4294967296" ), $lo ) );

        if ( function_exists("bcmul") )
            return bcadd ( bcmul ( $hi, "4294967296" ), $lo );

        // compute everything manually
        $a = substr ( $hi, 0, -5 );
        $b = substr ( $hi, -5 );
        $ac = $a*42949; // hope that float precision is enough
        $bd = $b*67296;
        $adbc = $a*67296+$b*42949;
        $r4 = substr ( $bd, -5 ) +  + substr ( $lo, -5 );
        $r3 = substr ( $bd, 0, -5 ) + substr ( $adbc, -5 ) + substr ( $lo, 0, -5 );
        $r2 = substr ( $adbc, 0, -5 ) + substr ( $ac, -5 );
        $r1 = substr ( $ac, 0, -5 );
        while ( $r4>100000 ) { $r4-=100000; $r3++; }
        while ( $r3>100000 ) { $r3-=100000; $r2++; }
        while ( $r2>100000 ) { $r2-=100000; $r1++; }

        $r = sprintf ( "%d%05d%05d%05d", $r1, $r2, $r3, $r4 );
        $l = strlen($r);
        $i = 0;
        while ( $r[$i]=="0" && $i<$l-1 )
            $i++;
        return substr ( $r, $i );         
    }

    list(,$a) = unpack ( "N", "\xff\xff\xff\xff" );
    list(,$b) = unpack ( "N", "\xff\xff\xff\xff" );
    $q = _Make64($a,$b);
    var_dump($q);

PHP int size is platform-dependent. There is a function called unpack() which essentially allows to convert different types of data from binary strings to PHP variables. It seems to be the only way to store as 64 bit is to store it as a string.

I found the following code at:
http://www.mysqlperformanceblog.com/2007/03/27/integers-in-php-running-with-scissors-and-portability/

/// portably build 64bit id from 32bit hi and lo parts
function _Make64 ( $hi, $lo )
{

        // on x64, we can just use int
        if ( ((int)4294967296)!=0 )
            return (((int)$hi)<<32) + ((int)$lo);

        // workaround signed/unsigned braindamage on x32
        $hi = sprintf ( "%u", $hi );
        $lo = sprintf ( "%u", $lo );

        // use GMP or bcmath if possible
        if ( function_exists("gmp_mul") )
            return gmp_strval ( gmp_add ( gmp_mul ( $hi, "4294967296" ), $lo ) );

        if ( function_exists("bcmul") )
            return bcadd ( bcmul ( $hi, "4294967296" ), $lo );

        // compute everything manually
        $a = substr ( $hi, 0, -5 );
        $b = substr ( $hi, -5 );
        $ac = $a*42949; // hope that float precision is enough
        $bd = $b*67296;
        $adbc = $a*67296+$b*42949;
        $r4 = substr ( $bd, -5 ) +  + substr ( $lo, -5 );
        $r3 = substr ( $bd, 0, -5 ) + substr ( $adbc, -5 ) + substr ( $lo, 0, -5 );
        $r2 = substr ( $adbc, 0, -5 ) + substr ( $ac, -5 );
        $r1 = substr ( $ac, 0, -5 );
        while ( $r4>100000 ) { $r4-=100000; $r3++; }
        while ( $r3>100000 ) { $r3-=100000; $r2++; }
        while ( $r2>100000 ) { $r2-=100000; $r1++; }

        $r = sprintf ( "%d%05d%05d%05d", $r1, $r2, $r3, $r4 );
        $l = strlen($r);
        $i = 0;
        while ( $r[$i]=="0" && $i<$l-1 )
            $i++;
        return substr ( $r, $i );         
    }

    list(,$a) = unpack ( "N", "\xff\xff\xff\xff" );
    list(,$b) = unpack ( "N", "\xff\xff\xff\xff" );
    $q = _Make64($a,$b);
    var_dump($q);
勿忘初心 2024-07-26 11:30:00

现在您应该获得 PHP 7 - 完全一致的 64 位支持。 不仅是整数,还包括所有 fstat、I/O 等。Windows 上的 PHP 7 是真正的 64 位。

Now you should get PHP 7 - fully consistent 64-bit support. Not only integers, but also all the fstat, I/O, etc. PHP 7 on Windows is true 64-bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文