3

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.

Kim
  • 2,070
  • 5
  • 33
  • 46

6 Answers6

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";
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • 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 Aug 06 '13 at 04:17
  • @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. – Treffynnon Aug 06 '13 at 08:47
  • 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 – Nico Haase Jan 27 '21 at 08:59
1

working solution :

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

It will return : 99999977706

Manoj Prajapat
  • 1,117
  • 1
  • 14
  • 27
0

For arbitrary length integers use GMP.

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
  • 1
    ...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! – Milan Jul 22 '15 at 19:09
0

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);
Avatar
  • 14,622
  • 9
  • 119
  • 198
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.

Michael Dillon
  • 1,037
  • 6
  • 16
  • Sorry,i modified it. $test is in fact a string and i need it to be a number for comparison purposes – Kim Jan 23 '12 at 15:30
  • 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. – Michael Dillon Jan 23 '12 at 15:34
-1

It can help you,

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

you can refer with this link

ShivarajRH
  • 902
  • 12
  • 24