Can PHP tell if the server os is 64-bit?
Note: This solution is a bit less convenient and slower than @Salman A's answer. I would advice you to use his solution and check for PHP_INT_SIZE == 8
to see if you're on a 64bit os.
If you just want to answer the 32bit/64bit question, a sneaky little function like this would do the trick (taking advantage of the intval function's way of handling ints based on 32/64 bit.)
<?phpfunction is_64bit(){ $int = "9223372036854775807"; $int = intval($int); if ($int == 9223372036854775807) { /* 64bit */ return true; } elseif ($int == 2147483647) { /* 32bit */ return false; } else { /* error */ return "error"; }}?>
You can see the code in action here: http://ideone.com/JWKIf
Note: If the OS is 64bit but running a 32 bit version of php, the function will return false (32 bit)...
To check the size of integer (4/8 bytes) you can use the PHP_INT_SIZE
constant. If PHP_INT_SIZE===8
then you have a 64-bit version of PHP. PHP_INT_SIZE===4
implies that a 32-bit version of PHP is being used but it does not imply that the OS and/or Processor is 32-bit.
On Windows+IIS there is a $_SERVER["PROCESSOR_ARCHITECTURE"]
variable that contains x86
when tested on my system (WinXP-32bit). I think it will contain x64
when running on a 64bit OS.
A slightly shorter and more robust way to get the number of bits.
strlen(decbin(~0));
How this works:
The bitwise complement operator, the tilde, ~, flips every bit.
@see http://php.net/manual/en/language.operators.bitwise.php
Using this on 0 switches on every bit for an integer.
This gives you the largest number that your PHP install can handle.
Then using decbin() will give you a string representation of this number in its binary form
@see http://php.net/manual/en/function.decbin.php
and strlen will give you the count of bits.
Here is it in a usable function
function is64Bits() { return strlen(decbin(~0)) == 64;}