3
$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?

Naftali
  • 144,921
  • 39
  • 244
  • 303
alex
  • 61
  • 1
  • 1
  • 5
  • `2147483647` is the highest number PHP can store in a 32 bit integer! See here: http://php.net/manual/language.types.integer.php – ComFreek Oct 16 '13 at 15:57
  • Are you running a 32 bit server? I tested your code on http://writecodeonline.com/php/ and it gives the results you expected. – Halcyon Oct 16 '13 at 15:57

1 Answers1

4

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.

SamA
  • 587
  • 3
  • 6
  • And the reason `$ts` displays properly is that PHP is calculating/storing it as a double-precision floating point number. – Sammitch Oct 16 '13 at 16:36