4

I have the following string which i need to convert to integer or bigint.

$test="99999977706";

I tried:

echo (int)$test;
echo (integer)$test;
echo intval($test);

But they are all returning me 2147483647.

How do i convert the above string to a number or bigint?

Many many thanks for all suggestions.

5
  • 1
    stackoverflow.com/questions/211345/… Commented Jan 23, 2012 at 15:26
  • Does it need to be an int or just a number? Because (platform dependent) casting to (float) may work. Commented Jan 23, 2012 at 15:27
  • I need it to be an int for mysql comparison purposes Commented Jan 23, 2012 at 15:29
  • One workaround which works for me was casting to float. Commented Jun 29, 2017 at 13:22
  • What do you want to achieve with that number? A usual integer cannot be larger than that value, and BigInt is not built in into PHP Commented Jan 27, 2021 at 9:00

6 Answers 6

2

MySQL isn't going to know the difference. Just feed the string into your query as though it is a number without first type casting it in PHP.

For example:

$SQL = "SELECT * FROM table WHERE id = $test";
Sign up to request clarification or add additional context in comments.

3 Comments

Hello @Treffynnon, How it I have to pre process the bigint before the SQL. Let's say I have to use select ... where ... in ( some bigint values ) ?
@Cauliturtle I have no idea what you are asking, but I suggest that you ask your own question on StackOverflow as comments aren't really designed for this kind of discussion.
Can you share more details about this? There is no connection to MySQL in the question, and whatever you want to do with the given ` $SQL` variable, you should not use it to run a query
2

working solution :

<?php
   $str = "99999977706";
   $bigInt = gmp_init($str);
   $bigIntVal = gmp_intval($bigInt);
   echo $bigIntVal;
   ?>

It will return : 99999977706

Comments

1

You can treat the string as string without converting it.

Just use a regex to leave only numbers:

$test = "99999977706";
$test = preg_replace('/[^0-9]/', '', $test);

Comments

0

In your snippet $test is already a number, more specifically a float. PHP will convert the contents of $test to the correct type when you use it as a number or a string, because the language is loosely typed.

2 Comments

Sorry,i modified it. $test is in fact a string and i need it to be a number for comparison purposes
If you cast it to a float you should be good to go. Mysql has no knowledge of php types so you should be fine casting it to a float and then using it accordingly.
0

For arbitrary length integers use GMP.

1 Comment

...probably the best solution but it should be pointed out that the GMP extension has to be installed or your gmp_init, gmp_intval etc. calls will fail!
-1

It can help you,

$token=  sprintf("%.0f",$appdata['access_token'] );

you can refer with this link

1 Comment

"Help" to achieve what? sprintf will never return a BigInt, but always a string

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.