Thursday 15 August 2013

32 bit - PHP intval() changes number NOT 2147483647 -



32 bit - PHP intval() changes number NOT 2147483647 -

i'm running php 5.3.13 , when execute

php -r "echo intval(9999999999);"

it outputs 1410065407.

when execute

php -r "echo intval(php_int_max);"

it outputs 2147483647.

the smaller integer causing code issues. why difference?

in case, value 9999999999 beingness stored float. can verify var_dump(9999999999). when it's casted signed integer, value truncated 32 bits gives value 1410065407.

you can verify calculation hand or using gmp math extension:

$num = gmp_init("9999999999"); $bits = gmp_pow(2, 32); var_dump(gmp_strval(gmp_mod($num, $bits))); // string(10) "1410065407"

php 32-bit

No comments:

Post a Comment