How to Check If Your PHP code is 64-bit?


The PHP7 is released and the performance compared to PHP5.x is at least two times faster! Another new feature of PHP7 is it has now 64-bit version so it can use more than 4GB RAM and code performance can be potentially two times faster depending on the application design. The following lists a few methods to check if your PHP code is running on 64-bit.

Use PHP_INT_SIZE constant

The PHP_INT_SIZE pre-defined constant returns the size of the int that the current PHP is built on, so if it is 8 bytes, then it is definitely 64-bit.

1
2
3
function is64bitPHP() {
    return PHP_INT_SIZE == 8;
}
function is64bitPHP() {
    return PHP_INT_SIZE == 8;
}

Check the Number of Bits

The expression ~0 returns the complements of 0 which is all one’s. So in 32-bit PHP, it should return 32 times of 1 and in 64-bit there should be 64 times of 1. Then we just need to count the number.

1
2
3
function is64bitPHP() {
    return strlen(decbin(~0)) == 64;
}
function is64bitPHP() {
    return strlen(decbin(~0)) == 64;
}

The function decbin converts a integer to its binary form and strlen counts the number of the string (PHP is dynamic-typed and will convert integer to string).

Check php_uname string

The php_uname returns the system/OS information, the parameter ‘m’ will ask for the machine type. In 64-bit, it will contain the string ‘x86_64’ so we can use this to test if it is 64-bit PHP.

1
2
3
function is64bitPHP() {
    return strstr(php_uname("m"), '64') == '64';
}
function is64bitPHP() {
    return strstr(php_uname("m"), '64') == '64';
}

Convert 2^64-1 from String to Number

If we can convert 2^64-1 which is “9223372036854775807” to number, it is 64-bit.

1
2
3
function is64bitPHP() {
    return intval("9223372036854775807") == 9223372036854775807;
}
function is64bitPHP() {
    return intval("9223372036854775807") == 9223372036854775807;
}
PHP How to Check If Your PHP code is 64-bit? php x64

Test if PHP is 64-bit

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
411 words
Last Post: Just a Simple Parallel Runner in C#
Next Post: PHP7 Shortens the Google Page Crawling Time

The Permanent URL is: How to Check If Your PHP code is 64-bit?

Leave a Reply