62

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    i was looking for more of a "every integer is 64-bit solution", so would rather not use a module for big integer. – nonopolarity May 15 '09 at 17:42
  • 1
    Side note: I found some 64-bit PHP binaries at: http://tommymontgomery.com/php64 – Lance Rushing Oct 19 '09 at 21:37
  • possible duplicate of [what are the specifics of setting up PHP so that integers will be 64-bit?](http://stackoverflow.com/questions/870401/what-are-the-specifics-of-setting-up-php-so-that-integers-will-be-64-bit) – Lightness Races in Orbit Feb 26 '12 at 15:06

5 Answers5

83

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
scotts
  • 4,027
  • 3
  • 29
  • 26
  • 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"? – nonopolarity May 15 '09 at 17:30
  • 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"): http://www.fusionxlan.com/PHPx64.php And if compiling from source, there is only one version for 32-bit and 64-bit.. – scotts May 16 '09 at 05:36
  • 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). – Bil Simser Jun 14 '09 at 01:44
  • 5
    As an aside, PHP cannot represent UNSIGNED integers. Thus the above number is actually 2^63 not the full available unsigned integer 2^64. – Matt S Jun 30 '10 at 18:04
  • 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.php – Westy92 Sep 29 '15 at 04:44
  • is it possible to force 64-bit integer on 32 bit machine? – NickSoft Oct 27 '15 at 14:09
  • 3
    Just tested the latest PHP 7 RC on 64-bit Windows, and it looks like they finally *finally* added consistent 64-bit integer support! – mindplay.dk Nov 18 '15 at 09:04
  • 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. – Derrek Bertrand Dec 29 '15 at 20:42
  • scotts, 32bit hardware? you meant x86 hardware... ? – Yousha Aleayoub Jan 19 '16 at 06:40
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tmont
  • 2,562
  • 20
  • 15
  • 3
    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? – Kannan Goundan Jan 27 '16 at 22:59
  • I'm also interested on how to get 64-bit int support on Windows – Mr Pablo Apr 11 '17 at 13:08
  • By now he means PHP 7, this is referred to as [Consistent 64-bit support](http://php.net/releases/7_0_0.php). – BenMorel Sep 15 '17 at 17:15
  • 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`. – Hanan Feb 13 '18 at 18:46
10

Maybe you could use either the GMP or BCMath extension.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
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);
Josh Curren
  • 10,171
  • 17
  • 62
  • 73
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anatol Belski
  • 660
  • 6
  • 8