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"
Problem :
In PHP, you want to convert[cast] a big integer value to string for display.
Solution :
Use GNU Multiple Precision's gmp_strval[] function to convert the big integer value to string.
For example :
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
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.
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.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Strings in PHP can be converted to numbers [float / int / double] very easily. In most use cases, it won’t be required since PHP does implicit type conversion. There are many methods to convert string into number in PHP some of them are discussed below:
Method 1: Using number_format[] Function. The number_format[] function is used to convert string into a number. It returns the formatted number on success otherwise it gives E_WARNING on failure.
Example:
Method 2: Using type casting: Typecasting can directly convert a string into float, double or integer primitive type. This is the best way to convert a string into number without any function.
Example:
Output:
1000 1000.314 1000.314
Method 3: Using intval[] and floatval[] Function. The intval[] and floatval[] functions can also be used to convert the string into its corresponding integer and float values respectively.
Example:
Method 4: By adding 0 or by performing mathematical operations. The string number can also be converted into an integer or float by adding 0 with the string. In PHP, performing mathematical operations, the string is converted to an integer or float implicitly.
Output:
1000.314 1000.314 1000.414
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.