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

5

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.

Taskwork answered 14/5, 2009 at 15:32 Comment(3)
i was looking for more of a "every integer is 64-bit solution", so would rather not use a module for big integer.Taskwork
Side note: I found some 64-bit PHP binaries at: tommymontgomery.com/php64Expressway
possible duplicate of what are the specifics of setting up PHP so that integers will be 64-bit?Roderic
H
85

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
Heeheebiejeebies answered 14/5, 2009 at 16:35 Comment(9)
i thought most new PC nowadays can run the 64-bit Windows Vista? Is that what is needed? 64-bit OS and then install a 64-bit version of PHP? at the download page of PHP i didn't see a 64-bit version. can someone points me to it? is it the "PHP 5.2.1 x64 Project"?Taskwork
I don't know the particulars of Vista (although, yes, you'll need the 64-bit OS too).. it looks like the only compiled binaries are available at this URL and not on the PHP site (found this as first result when Googling "64-bit PHP"): fusionxlan.com/PHPx64.php And if compiling from source, there is only one version for 32-bit and 64-bit..Heeheebiejeebies
The above only works on x64 hardware "with" an (unofficial) x64 version of PHP installed. Having the latest official 32-bit version installed on a x64 OS yields the first result (and you may have to use double-quotes to enter the above statement).Lepto
As an aside, PHP cannot represent UNSIGNED integers. Thus the above number is actually 2^63 not the full available unsigned integer 2^64.Busboy
Be careful. There are no 64-bit integers in PHP on Windows systems. "64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit." php.net/manual/en/language.types.integer.phpLandscapist
is it possible to force 64-bit integer on 32 bit machine?Japeth
Just tested the latest PHP 7 RC on 64-bit Windows, and it looks like they finally finally added consistent 64-bit integer support!Crusted
Like that beautiful example, now I can check that I have a 64 bit binary with almost no effort! Didn't know about that constant before now.Katheykathi
scotts, 32bit hardware? you meant x86 hardware... ?Partan
D
55

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.

Dinsmore answered 13/7, 2010 at 2:55 Comment(4)
What do you mean by "it does now"? I see that PHP 7 on Windows 64 supports 64-bit integers. Is there a PHP 5.x release that does also? If so, where did you get it?Doreathadoreen
I'm also interested on how to get 64-bit int support on WindowsReactor
By now he means PHP 7, this is referred to as Consistent 64-bit support.Croak
Using wamp 3.1.0 64bit I'm able to get 64 bit integers on a x64 bit Windows 10 machine. It includes PHP 7.1.9.Plucky
F
10

Maybe you could use either the GMP or BCMath extension.

Friction answered 14/5, 2009 at 15:41 Comment(0)
P
9

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);
Pesce answered 14/5, 2009 at 15:35 Comment(0)
H
4

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.

Homocercal answered 16/10, 2015 at 12:16 Comment(3)
I just upgraded to PHP 7 and 64-bit integers still aren't available. Any idea what I can do?Release
Clifton, be sure you're using a 64-bit build.Homocercal
Yeah, it turns out my Ubuntu server was only 32-bit. I had to upgrade.Release

© 2022 - 2024 — McMap. All rights reserved.