Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string to int cast php change value [duplicate]

Tags:

php

$ms = microtime(true);
$ts = $ms * 10;
$i = substr($ts, 0,strpos($ts, "."));
echo "    A: ". $ms;
echo "    B: ". $ts;

echo "    C: ". $i; 
echo "    D: ". intval($i); 
echo "    E: ". (int)$i; 

example:

A: 1382292940.8799
B: 13822929408.799
C: 13822929408
D: 2147483647
E: 2147483647

But

E =/= C && D =/= C

Why does this happen?

like image 373
alex Avatar asked Jan 30 '26 17:01

alex


1 Answers

The problem you are having is an overflow one. 32-bit integers hold up to 4 billion and some unsigned and only 2 billion and some signed. The number you are converting to an integer is much larger than that:

13,822,929,408

Hence you are seeing 2147483647, the limit for a 32-bit signed integer.

like image 144
SamA Avatar answered Feb 02 '26 11:02

SamA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!