22

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.

6
  • Are you using this number for calculation or as display only? 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. Commented Nov 12, 2011 at 0: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. Commented Nov 12, 2011 at 0: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... Commented Nov 12, 2011 at 0:38
  • Looks like this is covered pretty well in stackoverflow.com/questions/990406/… Commented Nov 12, 2011 at 0:46
  • Also, when you upload it to the server, if its a 64bit you should have no problem in casting bigger ints there... Commented Feb 8, 2013 at 4:46

7 Answers 7

11

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Is there a specific GMP function that will do what the OP asked?
@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).
6

I solved it by casting to float rather than int:

(int) '8554104470' => 2147483647

(float) '8554104470' => 8554104470

Comments

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.

Comments

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;
}

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.