0

I am seeing strange behavior with unsigned integer variables on PHP. Following code:

$uint64 = 11689184649718456000;
$int64 = (int)$uint64;

var_dump($uint64);
var_dump($int64);
$str = number_format($uint64,0,"","");
var_dump($str);

Output is:

float(1.1689184649718E+19)
int(-6757559423991095296)
string(20) "11689184649718456320"

What's causing the wrong outputs here?

In the End: What do I want to achieve?

  1. Receive an unsigned integer (uint64) via API
  2. Convert it to an unsigned integer
  3. Store it in a database (because PostgreSQL doesn't support unsigned integers)
  4. Get it from the Database
  5. Convert it back to an unsigned integer (uint64)
Nick
  • 138,499
  • 22
  • 57
  • 95
Crack_David
  • 2,775
  • 1
  • 14
  • 30
  • 4
    ['*If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.*'](http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow) - However, with your current requirements, I believe your question is too broad. Though not ideal, consider storing them as string if you cannot find another suitable data type and on output manipulate them with some math library. – Script47 Mar 22 '19 at 10:53
  • Also: [What's the maximum value for an int in PHP?](https://stackoverflow.com/questions/670662/whats-the-maximum-value-for-an-int-in-php) – Script47 Mar 22 '19 at 10:59

0 Answers0