Php: How to Convert Bigint from Int to String

PHP: How to convert bigint from int to string?

You actually should ensure that you type what you mean:

$bigint = 9999999999999999999;

Is not a PHP integer but float:

float(1.0E+19)

If you would have done

$bigint = (int) 9999999999999999999;

You would have set an integer in fact, but it would not be the number you might have expected:

int(-8446744073709551616)

It's no problem at all to turn that into string as you might have guessed. So take care when you write numbers into code that you actually write what you mean.

See this line again:

$bigint = 9999999999999999999;

try to understand what you have actually written. It's not an integer at all because PHP will turn it into a float. See Example #4 Integer overflow on a 64-bit system in the integer manual page.

If you need higher precision, checkout GNU Multiple Precision, it might have what you're looking for. However, this won't change how to write numbers in PHP:

$bigint = gmp_init("9999999999999999999");
$bigint_string = gmp_strval($bigint);
var_dump($bigint, $bigint_string);

Output:

resource(4) of type (GMP integer)
string(19) "9999999999999999999"

Convert a big integer to a full string in PHP

This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.

It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.

Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.

It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.

$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;

php: int() function equivalent for bigint type? (int() cuts strings to 2147483647)

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.

PHP -- Convert string to bigint

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

PHP: Converting big integer from string to int with overflow

here is the workaround.

<?php
$unsignedString = "3000000000";
echo "unsigned string: ".$unsignedString."<br />";
$signedInt = intval(doubleval($unsignedString));
echo "signed int: ".$signedInt."<br />";
?>

doctrine is fetching bigints as string - how to prevent this

One solution would be to override Doctrine's implementation of bigint type with your own. First, create a class identical to Doctrine's BigIntType, except replacing the cast to string with a cast to int:

class MyBigIntType extends Type
{
/**
* {@inheritdoc}
*/
public function getName()
{
return Type::BIGINT;
}

/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getBigIntTypeDeclarationSQL($fieldDeclaration);
}

/**
* {@inheritdoc}
*/
public function getBindingType()
{
return \PDO::PARAM_STR;
}

/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return (null === $value) ? null : (int)$value;
}
}

Then, register the type in config.yml or doctrine.yaml:

doctrine:
dbal:
types:
bigint: MyBigIntType

I tested this with Symfony 2.8, and this caused to use MyBigIntType for all fields that were of type bigint. Should work with later Symfony versions as well.

Convert BIGINT result from MySQL COUNT() to INT

Your problem is not that PHP can't support a BIGINT, but that you're running a 32-bit PHP version that can't support numbers as large as you're trying to accommodate.

The best option would be to upgrade to a 64-bit version, because how are you even running 32bit at this point in time?

The less-good, if you're really stuck, option is to:

  1. Cast the number to a string to get it out of mysql and into PHP:

    SELECT CAST(COUNT(Date) AS CHAR(32)) AS RowCount FROM `Health_Data` WHERE Weight < 200;
  2. Use BC Math functions if you need to do math on those numbers in PHP.



Related Topics



Leave a reply



Submit