检查您的代码是否在 64 位 PHP 上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5423848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if your code is running on 64-bit PHP
提问by binarycleric
Does anyone know of a way of checking within PHP if the script is running as either 32-bit or 64-bit? Currently I'm using PHP 5.3.5.
如果脚本以 32 位或 64 位运行,有人知道在 PHP 中检查的方法吗?目前我使用的是 PHP 5.3.5。
Ideally I'd like to write a function so my code can look like this:
理想情况下,我想编写一个函数,以便我的代码如下所示:
if( is_32bit() === true ) {
do_32bit_workaround();
}
do_everything_else();
Anyone have any thoughts?
有人有什么想法吗?
回答by coreyward
Check the PHP_INT_SIZE
constant. It'll vary based on the size of the register (i.e. 32-bit vs 64-bit).
检查PHP_INT_SIZE
常数。它会根据寄存器的大小(即 32 位与 64 位)而变化。
In 32-bit systems PHP_INT_SIZE
should be 4, for 64-bit it should be 8.
在 32 位系统中PHP_INT_SIZE
应该是4,对于 64位系统应该是8。
See http://php.net/manual/language.types.integer.phpfor more details.
有关更多详细信息,请参阅http://php.net/manual/language.types.integer.php。
回答by Tim Penner
You could write a function like this:
你可以写一个这样的函数:
function is_32bit(){
return PHP_INT_SIZE === 4;
}
Then you could use the sample code you posted:
然后您可以使用您发布的示例代码:
if ( is_32bit() ) {
do_32bit_workaround();
} else {
do_everything_else();
}
回答by Chris0
A short 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
@see http://php.net/manual/en/language.operators.bitwise.php
Using this on 0switches on every bit for an integer.
在0上使用它会打开整数的每一位。
This gives you the largest number that your PHP install can handle.
这为您提供了 PHP 安装可以处理的最大数量。
Then using decbin() will give you a string representation of this number in its binary form
然后使用 decbin() 将以二进制形式为您提供此数字的字符串表示
@see http://php.net/manual/en/function.decbin.php
@see http://php.net/manual/en/function.decbin.php
and strlen will give you the count of bits.
和 strlen 会给你位数。
Here is it in a usable function
这是一个可用的功能
function is32Bits() {
return strlen(decbin(~0)) == 32;
}
回答by naT erraT
Here is an example that can be used from console
这是一个可以从控制台使用的示例
For Windows:
对于 Windows:
php -r "echo (PHP_INT_SIZE == 4 ? '32 bit' : '64 bit').PHP_EOL;" && php -i | findstr Thread
For Linux
对于 Linux
php -r "echo (PHP_INT_SIZE == 4 ? '32 bit' : '64 bit').PHP_EOL;" && php -i | grep Thread
Output example:
输出示例:
64 bit
Thread Safety => disabled
回答by Jon
I just looked around and didn't find anything too promising. There's a good chance that you can use $_SERVER['SERVER_SOFTWARE']
to tell (check out what it prints on your system), but making this portable and always accurate is probably not doable.
我只是环顾四周,没有发现任何有希望的东西。很有可能您可以使用它$_SERVER['SERVER_SOFTWARE']
来判断(检查它在您的系统上打印的内容),但是使其便携且始终准确可能是不可行的。