如果脚本以32位或64位运行,有没有人知道在PHP中检查的方法?目前我正在使用PHP 5.3.5.
理想情况下,我想编写一个函数,所以我的代码看起来像这样:
if( is_32bit() === true ) {
do_32bit_workaround();
}
do_everything_else();
Run Code Online (Sandbox Code Playgroud)
有人有什么想法?
cor*_*ard 89
检查PHP_INT_SIZE常数.它会根据寄存器的大小而变化(即32位与64位).
在32位系统中PHP_INT_SIZE应为4,对于64位应为8.
有关详细信息,请参见http://www.php.net/manual/en/language.types.integer.php.
你可以写一个这样的函数:
function is_32bit(){
return PHP_INT_SIZE === 4;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用你发布的示例代码:
if ( is_32bit() ) {
do_32bit_workaround();
} else {
do_everything_else();
}
Run Code Online (Sandbox Code Playgroud)
获取位数的一种简短方法。
strlen(decbin(~0));
Run Code Online (Sandbox Code Playgroud)
这是如何工作的:
按位补码运算符,波浪号~,翻转每一位。
@see http://php.net/manual/en/language.operators.bitwise.php
在0上使用它会打开整数的每一位。
这为您提供了 PHP 安装可以处理的最大数量。
然后使用 decbin() 将以二进制形式为您提供此数字的字符串表示
@see http://php.net/manual/en/function.decbin.php
和 strlen 会给你位数。
这是一个可用的功能
function is32Bits() {
return strlen(decbin(~0)) == 32;
}
Run Code Online (Sandbox Code Playgroud)
这是一个可以从控制台使用的示例
对于 Windows:
php -r "echo (PHP_INT_SIZE == 4 ? '32 bit' : '64 bit').PHP_EOL;" && php -i | findstr Thread
Run Code Online (Sandbox Code Playgroud)
对于 Linux
php -r "echo (PHP_INT_SIZE == 4 ? '32 bit' : '64 bit').PHP_EOL;" && php -i | grep Thread
Run Code Online (Sandbox Code Playgroud)
输出示例:
64 bit
Thread Safety => disabled
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34486 次 |
| 最近记录: |