Checking if your code is running on 64-bit PHP

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.

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?


Solution 1:

Check the PHP_INT_SIZE constant. It'll vary based on the size of the register (i.e. 32-bit vs 64-bit).

In 32-bit systems PHP_INT_SIZE should be 4, for 64-bit it should be 8.

See http://php.net/manual/language.types.integer.php for more details.

Solution 2:

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