-1

pay attention the integer is bigger than PHP_INT_MAX (9223372036854775807)

Code:

  $x = 1502648031311810478;
  var_dump($x);

Output:

float 1.502648031381E+16

I want this:

1502648031311810478

as a string.

I tried all ways of casting but output is 1.502648031381E+16

I want exact integer in string.

This is not a duplicate of Converting an integer to a string in PHP because none of the answers to that question cover the case when the number is outside PHP_INT_MAX.

Salman A
  • 262,204
  • 82
  • 430
  • 521
Mohsen
  • 1,295
  • 1
  • 15
  • 45
  • 2
    Not reproducing: https://3v4l.org/oG43I – Madhur Bhaiya Nov 12 '18 at 07:46
  • 1
    @MadhurBhaiya try it with another 0. There seems to be a comment [here](http://php.net/manual/en/function.intval.php#61292) without a solution. – Nigel Ren Nov 12 '18 at 07:54
  • @AlivetoDie it is not a Dupe. None of the answers in the duplicate marked link cover the case when the number is outside `PHP_INT_MAX`. – Madhur Bhaiya Nov 12 '18 at 08:02
  • @AlivetoDie agreed that OP's example is wrong. But his idea for asking this on right track. Please check the discussion above. – Madhur Bhaiya Nov 12 '18 at 08:05
  • @MadhurBhaiya question updated – Mohsen Nov 12 '18 at 08:10
  • 2
    Where exactly is the number coming from? The question cannot be answered properly without this information. – Salman A Nov 12 '18 at 08:13
  • @SalmanA this is the number that i want to save to db to use somewhere else but i need exact number to use. something like it's pointer – Mohsen Nov 12 '18 at 08:24
  • @mohsen apparently you're using 32-bit PHP so it cannot represent this number as int, it will be auto-converted to float. You can specify the number as string though `$x = '1502648031311810478';` which is why I asked where it comes from. – Salman A Nov 12 '18 at 08:27
  • @SalmanA i use 64bit php and number come from some web service that use this long number as pointer to other methods and i just want to save it and use it in future. – Mohsen Nov 12 '18 at 08:30
  • @mohsen is it coming as json (and you're running json_decode on it)? And are you sure about PHP being 64 bit (check `echo PHP_INT_MAX;` and post what it returns) – Salman A Nov 12 '18 at 08:36
  • @SalmanA exaclty coming from json and running json_decode – Mohsen Nov 12 '18 at 09:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183489/discussion-between-salman-a-and-mohsen). – Salman A Nov 12 '18 at 09:46

1 Answers1

3

Seems like you're using json_decode. It will convert numbers to integers where possible, otherwise float:

// 32bit integers
var_dump(json_decode("2147483647")); // int(2147483647)
var_dump(json_decode("2147483648")); // float(2147483648)

// 64bit integers
var_dump(json_decode("9223372036854775807")); // int(9223372036854775807)
var_dump(json_decode("9223372036854775808")); // float(9.2233720368548E+18)

You can use JSON_BIGINT_AS_STRING flag so that json_decode decodes large integers as their original string value:

// 32bit integers
var_dump(json_decode("2147483647", false, 512, JSON_BIGINT_AS_STRING)); // int(2147483647)
var_dump(json_decode("2147483648", false, 512, JSON_BIGINT_AS_STRING)); // string(10) "2147483648"
// 64bit integers
var_dump(json_decode("9223372036854775807", false, 512, JSON_BIGINT_AS_STRING)); // int(9223372036854775807)
var_dump(json_decode("9223372036854775808", false, 512, JSON_BIGINT_AS_STRING)); // string(19) "9223372036854775808"

Note that the number-as-a-string is not really useful for arithmetic e.g. you cannot add or multiply from it without it (auto) converting to float.

Salman A
  • 262,204
  • 82
  • 430
  • 521