简体   繁体   English

如何确定我的操作系统是32位还是64位?

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

I tried using <?php echo php_uname("m"); ?> 我尝试使用<?php echo php_uname("m"); ?> <?php echo php_uname("m"); ?> , it returns i586 but I am on Windows 7 64 bit which I could see in My Computers Properties. <?php echo php_uname("m"); ?> ,它返回i586,但我在Windows 7 64位,我可以在我的计算机属性中看到。 So I am expecting x86_64 in output. 所以我期待输出中的x86_64 Does any one know how to determine OS Architecture in PHP ? 有谁知道如何在PHP中确定OS体系结构?

I want the same thing for Mac OS X too. 对于Mac OS X我也想要同样的东西。 Any help would be appreciated. 任何帮助,将不胜感激。

这是一个php解决方案:)

echo strlen(decbin(~0));

更简单

echo 8 * PHP_INT_SIZE;

My best guess is that even though your OS is 64bit, your Webserver is x86 and runs in WOW64-mode (32bit). 我最好的猜测是,即使您的操作系统是64位,您的Web服务器也是x86,并以WOW64模式(32位)运行。 If that's the case, it should be hard to figure out in pure PHP. 如果是这种情况,那么在纯PHP中应该很难理解。

My suggestion (thanks to Leigh for linking to a similar question) is to use WMI: 我的建议(感谢Leigh链接到类似的问题)是使用WMI:

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

php_uname checks the operating mode of PHP not of the OS . php_uname检查PHP操作模式而 不是操作系统

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 . 因此,如果您的操作系统是64位,但php_uname返回i586,这是因为您运行的是32位版本的PHP

Knowing the architecture of PHP is arguably more important than knowing the architecture of the OS. 了解PHP的体系结构可能比了解操作系统的体系结构更重要。 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). 例如,如果您依赖于64位操作系统来在代码中做出决策,那么当您以32位模式执行PHP时,您可能会发现自己编写的代码在64位操作系统上失败(就像您现在一样)。 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. 这实际上是你所处的精确情况,你期望64位结果但得到32位的结果,这是因为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. 您的问题的真正解决方案是在64位操作系统上下载并安装64位版本的PHP,以便查看64位结果。


Here is a simple 1 line of code to detect if PHP is executing in 64 bit or 32 bit: 这是一行简单的代码,用于检测PHP是以64位还是32位执行:

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

After executing the line above $php64bit will be either true or false . 执行$php64bit以上的行后,将为truefalse

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. 这通过检查php_uname是否存在64来实现,如果PHP在64位模式下执行则会找到该64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM