Jigar D
Jigar D

Reputation: 83

How can I determine whether my OS is 32 or 64 bit?

I tried using <?php echo php_uname("m"); ?>, it returns i586 but I am on Windows 7 64 bit which I could see in My Computers Properties. So I am expecting x86_64 in output. Does any one know how to determine OS Architecture in PHP ?

I want the same thing for Mac OS X too. Any help would be appreciated.

Upvotes: 4

Views: 4904

Answers (4)

Tim Penner
Tim Penner

Reputation: 3541

php_uname checks the operating mode of PHP not of the OS.

So if your OS is 64 bit but php_uname is returning i586 that is because you are running the 32 bit version of PHP.

Knowing the architecture of PHP is arguably more important than knowing the architecture of the OS. For example, if you rely on the OS being 64 bit to make decisions in code, you may find yourself writing code that fails on 64 bit OS when PHP executing in 32 bit mode (as you are now). This is actually the precise situation you are in, you were expecting a 64 bit result but getting a result for 32 bit, which is because of the operating mode of PHP.

The real solution for your issue is to download and install the 64 bit version of PHP on your 64 bit OS in order to see the 64 bit results.


Here is a simple 1 line of code to detect if PHP is executing in 64 bit or 32 bit:

empty(strstr(php_uname("m"), '64')) ?  $php64bit = false : $php64bit = true;

After executing the line above $php64bit will be either true or false.

Here is a multi-line version of the same code:

// detect which version of PHP is executing the script (32 bit or 64 bit)
if(empty(strstr(php_uname("m"), '64'))){
  $php64bit = false;
}else{
  $php64bit = true;
}

This works by checking php_uname for the presence of 64 which would be found if PHP was executing in 64 bit mode.

Upvotes: 1

user3665589
user3665589

Reputation: 81

more simple

echo 8 * PHP_INT_SIZE;

Upvotes: 8

alecsammon
alecsammon

Reputation: 109

Here is a php solution :)

echo strlen(decbin(~0));

Upvotes: 10

Niklas B.
Niklas B.

Reputation: 95278

My best guess is that even though your OS is 64bit, your Webserver is x86 and runs in WOW64-mode (32bit). If that's the case, it should be hard to figure out in pure PHP.

My suggestion (thanks to Leigh for linking to a similar question) is to use WMI:

$out = array();
exec("wmic cpu get DataWidth", $out);
$bits = strstr(implode("", $out), "64") ? 64 : 32;
echo $bits; // 32 or 64

Upvotes: 1

Related Questions