how to have 64 bit integer on PHP?

2019-01-01 01:09发布

问题:

does someone know how to have 64 bit integer on PHP? it seems like it is not by config file but rather it might be a compile time option and it depends on the platform?

回答1:

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


回答2:

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 64bit long so PHP_INT_MAX is going to be 9223372036854775807.



回答3:

Maybe you could use either GMP or BCMath extension.



回答4:

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


回答5:

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