Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP -- Convert string to bigint

Tags:

php

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.

like image 766
Kim Avatar asked Oct 19 '25 01:10

Kim


2 Answers

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";
like image 181
Treffynnon Avatar answered Oct 21 '25 15:10

Treffynnon


working solution :

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

It will return : 99999977706

like image 27
Manoj Prajapat Avatar answered Oct 21 '25 17:10

Manoj Prajapat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!