15

I am using PHP 5.3.

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 bytes (32 bits)

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.

Is there a way to force "(int)" cast to use the 32-bit size?

For example, assume the following code:

$test = 4170266799;
print $test;                     
print (int) $test;

On my 32-bit system, the output is:

4170266799
-124700497

On my 64-bit system, the output is:

4170266799
4170266799

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Mick
  • 13,248
  • 9
  • 69
  • 119
  • 7
    I would fix the encoding algorithm... – Karoly Horvath Nov 14 '11 at 22:05
  • 1
    Is this an instance of [the XY Problem](http://meta.stackexchange.com/q/66377/164291)? What problem are you *really* trying to solve? –  Nov 16 '11 at 04:58
  • No, it is not an instance of the XY problem. I am converting portions of a large code base written in native compiled code. Part of that I am moving to PHP to be accessed from the web, however it must generate information in the same manner expected by my 32-bit application. That is why I rely on the INT size. I am choosing to do as close a conversion as possible instead of redesigning the logic to generate the same results. Fortunately, I now have it working. – Mick Nov 16 '11 at 14:33
  • @Mick, you can always use a pre-compiler that does the modification for you. – Pacerier Jun 06 '13 at 15:16

2 Answers2

4

No, this is very much dependant upon the platform itself:

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers.

And because the values that expose the integer size and integer maximum are constants, they can't be changed - being determined at compile-time of PHP based on the current system.

Not much consolation, but this is why relying on something such as a constant (that isn't yours to change, and is subject to change per build), or any kind of "sizeof` implementations are just as evil as magic numbers in code - don't do it. One thing that we can be sure of (at least, I hope!) is that 4 is and always will be 4, but not that x, y, or z will represent 4, even if they do now.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 1
    Thank you for your response. You are correct that I cannot change the size of an INT in PHP. Fortunately, I have found a way to "cast" an INT to the size I expect on a 32-bit system, even when my code is running on a 64-bit system. – Mick Nov 15 '11 at 02:57
  • Do tell us how, Mick. – A.Grandt Nov 01 '15 at 12:21
4

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);

And the output on the 32-bit system is:

4170266799  # $test = 4170266799
-124700497  # (int) $test
-124700497  # thirtyTwoBitIntval($test);

And the output on the 64-bit system is:

4170266799  # $test = 4170266799
4170266799  # (int) $test
-124700497  # thirtyTwoBitIntval($test);
Community
  • 1
  • 1
Mick
  • 13,248
  • 9
  • 69
  • 119
  • 3
    For me, your solution does not work, tests do fail. This solution does work: http://stackoverflow.com/a/2123458/1140922 – Olha Puzhay Jan 08 '13 at 15:59