17

php: What's the equivalent int() function for bigint type? (int() cuts big numbers to 2147483647)?

Example:

$bigint1="12312342306A_C243";
$bigint1=(int)$bigint1;//2147483647

but I want it to be 12312342306.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • Are you using this number for calculation or as display only? http://php.net/manual/en/language.types.integer.php recommends casting as float if you really need a number but it does come with some caveats. – Jim H. Nov 12 '11 at 00:12
  • 2
    Sounds like you are running php on a 32 bit system. PHP_INT_MAX will tell you what the largest integer can be. Numbers > PHP_INT_MAX will be transformed into floats. – FlyingGuy Nov 12 '11 at 00:14
  • I want to be sure it contains numbers only, and if not, then cut it up to the first non-integer symbol. The result will be put into MySQL db as bigint (int is not enough.) FlyungGuy, you are right, 32 bit system:) That's the local machine, then it will be uploaded to the server... – Haradzieniec Nov 12 '11 at 00:38
  • Looks like this is covered pretty well in http://stackoverflow.com/questions/990406/php-intval-equivalent-for-numbers-2147483647 – Frank Farmer Nov 12 '11 at 00:46
  • Also, when you upload it to the server, if its a 64bit you should have no problem in casting bigger ints there... – Manatax Feb 08 '13 at 04:46
  • I ran into the problem with converting timestamps later than January 2038 but now the last timestamp is in year 29,227,704,433 :) So it shouldn't be a problem anymore. The PHP_INT_MAX on 64bit is [9223372036854775807](https://util.host/timestamp?t=9223372036854775807). You can check out of bounds with bccomp. – DerFichtl May 12 '21 at 23:20

7 Answers7

9

I know is old and answered, but for future reference of people looking at this:

UPDATED

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX since PHP 5.0.5, and minimum value using the constant PHP_INT_MIN since PHP 7.0.0.

Source

Manatax
  • 4,083
  • 5
  • 30
  • 40
6

There isn't a built-in type to do this kind of cast as you can see here (from the official doc). Anyway, you can use the GMP library to manage this long int.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
  • 1
    Is there a specific GMP function that will do what the OP asked? – Frank Farmer Nov 12 '11 at 00:34
  • @FrankFarmer It really depends on what the OP wants to do with that number. In his question, he only shows a cast. Anyway, to manage that kind of int, GMP is a good library to use (just like BCMath). – Aurelio De Rosa Nov 12 '11 at 00:38
4

I solved it by casting to float rather than int:

(int) '8554104470' => 2147483647

(float) '8554104470' => 8554104470

Menno Bieringa
  • 1,215
  • 12
  • 17
2

This may not be the kind of answer you wanted, but, why not switch to a 64-bit machine?

On my (64-bit Fedora) PC $bigint1 has the value 12312342306 as you desired.

tialaramex
  • 3,761
  • 1
  • 21
  • 23
2

It's obviously not possible to replicate the function of (int) exactly for 64-bit numbers on a 32-bit system. (int) returns an int; the best you can do on a 32-bit system for 64-bit numbers is return a string -- but presumably this is really what you're asking for.

As tialaramex pointed out, if you're going to do a lot of work with 64 bit integers, you should run 64 bit PHP. If that's not an option, you can do some work using the bcmath functions -- although I don't see a bcmath function for doing what you've asked.

I'd just write a quick and dirty function:

<?php
var_dump(toint("12312342306A_C243"));
function toint($str) {
        return preg_match('/^[0-9]+/', $str, $matches) ? $matches[0] : 0;
}
Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
1
$bigint1="12312342306A_C243";
$bigint1=(int)$bigint1;//2147483647

If you want just the number until the first char which is not a number( as you mention you just want 12312342306) then I suggest just cut that string, take what you want, and just then cast it into int.

function parseBigInt(string $bigInt, string $delimiter = '_'): int
{
  if (false !== ($pos = strpos($bigInt, $delimiter))) {
      $bigInt = substr($bigInt, 0, $pos);
  }

  return (int)$bigInt;
}

$bigInt = parseBigInt('12312342306A_C243'); // '12312342306' as int
Chemaclass
  • 1,933
  • 19
  • 24
0

I am using bcmath functions for all operations with integer of size > 4 bytes. You must keep in mind, all operands and results are strings, but easy usable for displaying or passing to database as argument values.

Hink
  • 1,054
  • 1
  • 15
  • 31