Qrzysio
Qrzysio

Reputation: 1172

String to integer conversion in PHP changes the value

So I have this simple code:

$var = abs(crc32('string - 8 characters')).rand(100,999);
var_dump($var);
echo '<br>';
var_dump((int)$var);
echo '<br>';
var_dump(intval($var));

And the result:

string(13) "2047654753402"
int(2147483647)
int(2147483647) 

Why the integer value is 2147483647? I expected it to be 2047654753402. I just want to convert the string to integer without changing the value.

Upvotes: 1

Views: 436

Answers (1)

Tony
Tony

Reputation: 204

You can not convert large string number to integer because interger has limitation -2147483647 to 2147483647. So If you want to convert number string to integer which has out of range of integer length, you can use double.

$var = abs(crc32('string - 8 characters')).rand(100,999);
var_dump($var);
echo '<br>';
var_dump((double)$var)

Upvotes: 1

Related Questions