How to convert a string into number in PHP?
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:
<?php $num = "1000.314"; // Convert string in number using // number_format(), function echo number_format($num), "\n"; // Convert string in number using // number_format(), function echo number_format($num, 2); ?> |
1,000 1,000.31
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:
<?php // Number in string format $num = "1000.314"; // Type cast using int echo (int)$num, "\n"; // Type cast using float echo (float)$num, "\n"; // Type cast using double echo (double)$num; ?> |
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:
<?php // Number in string format $num = "1000.314"; // intval() function to convert // string into integer echo intval($num), "\n"; // floatval() function to convert // string to float echo floatval($num); ?> |
1000 1000.314
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.
<?php // Number into string format $num = "1000.314"; // Performing mathematical operation // to implicitly type conversion echo $num + 0, "\n"; // Performing mathematical operation // to implicitly type conversion echo $num + 0.0, "\n"; // Performing mathematical operation // to implicitly type conversion echo $num + 0.1; ?> |
1000.314 1000.314 1000.414
Recommended Posts:
- How to convert string to boolean in PHP?
- Convert a string to an integer in JavaScript
- Convert string into date using JavaScript
- JavaScript | Convert a string to boolean
- How to convert an object to string using JavaScript?
- How to convert string into float in JavaScript?
- How to convert an Array to String in Java?
- Convert a String to a List of Characters in Java
- Convert List of Characters to String in Java
- How to convert string to camel case in JavaScript ?
- Convert a String to Character array in Java
- Convert string to title case in JavaScript
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert seconds to time string format hh:mm:ss using JavaScript ?
- Convert comma separated string to array using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



