我可以在运行PHP的64位系统上将PHP_INT_SIZE定义为4个字节吗? [英] Can I define PHP_INT_SIZE to 4 bytes on 64-bit system running PHP?
问题描述
我使用的是PHP 5.3。
I am using PHP 5.3.
在我的32位系统上,INT的大小:
On my 32-bit system the size of an INT:
print "PHP_INT_MAX: " . PHP_INT_MAX . "\n";
print "PHP_INT_SIZE: " . PHP_INT_SIZE . " bytes (" . (PHP_INT_SIZE * 8) . " bits)\n";
- PHP_INT_MAX: 2147483647
- PHP_INT_SIZE: 4字节(32位)
- PHP_INT_MAX: 2147483647
- PHP_INT_SIZE: 4 bytes (32 bits)
但是,部分我使用的编码算法依赖于int是上述大小(4个字节)的事实。当我在我的web主机服务器上运行代码时,它是一个64位系统,int大小是两倍大。
However, part of an an encoding algorithm I am using relies on the fact that an int is the above size (4 bytes). When I run the code on my web host's server, it is a 64-bit system and the int size is twice as large.
有没有办法强迫( int)强制转换为使用32位大小?
Is there a way to force "(int)" cast to use the 32-bit size?
例如,假设以下代码:
$test = 4170266799;
print $test;
print (int) $test;
在我的 32位系统上,输出为:
On my 32-bit system, the output is:
4170266799
-124700497
在我的 64位系统上,输出为:
On my 64-bit system, the output is:
4170266799
4170266799
是否可以强制INT的值为4个字节,甚至当架构从32位更改为64位时?
Is it possible to force the value of an INT to be 4 bytes, even when the architecture changes from 32-bit to 64-bit?
推荐答案
引用这个问题关于Stack Overflow,我找到了一个似乎在我的早期测试中有效的解决方案:
Referencing this question on Stack Overflow, I have found a solution that seems to work in my early tests:
function thirtyTwoBitIntval($value)
{
if ($value < -2147483648)
{
return -(-($value) & 0xFFFFFFFF);
}
elseif ($value > 2147483647)
{
return ($value & 0xFFFFFFFF);
}
return $value;
}
$test = 4170266799;
print $test;
print (int) $test;
print thirtyTwoBitIntval($test);
32位系统的输出为:
4170266799 # $test = 4170266799
-124700497 # (int) $test
-124700497 # thirtyTwoBitIntval($test);
64位系统的输出为:
4170266799 # $test = 4170266799
4170266799 # (int) $test
-124700497 # thirtyTwoBitIntval($test);
这篇关于我可以在运行PHP的64位系统上将PHP_INT_SIZE定义为4个字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!