Php convert string to bigint

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.

asked Jan 23, 2012 at 15:23

KimKim

2,0625 gold badges33 silver badges46 bronze badges

5

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

answered Jan 23, 2012 at 15:35

TreffynnonTreffynnon

21k6 gold badges62 silver badges97 bronze badges

3

working solution :


It will return : 99999977706

answered Sep 1, 2018 at 5:25

Manoj PrajapatManoj Prajapat

1,1171 gold badge14 silver badges26 bronze badges

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.

answered Jan 23, 2012 at 15:27

2

For arbitrary length integers use GMP.

answered Mar 4, 2014 at 3:35

Markus MalkuschMarkus Malkusch

7,5502 gold badges37 silver badges63 bronze badges

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

answered Jan 27, 2021 at 8:31

AvatarAvatar

13.3k8 gold badges112 silver badges183 bronze badges

It can help you,

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

you can refer with this link

answered Feb 7, 2013 at 10:57

ShivarajRHShivarajRH

87211 silver badges23 bronze badges

1

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

Problem :

In PHP, you want to convert[cast] a string to become big integer value.

Solution :

Use GNU Multiple Precision's gmp_intval[] function to convert the string to a big integer value.

For example :

 

Output :

123456789123456789

Notes :

See //php.net/manual/en/gmp.installation.php on how to get GMP installed on your server.

Reference :

//php.net/manual/en/book.gmp.php

//php.net/manual/en/function.gmp-intval.php

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

Chủ Đề