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.
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.
1000 1000.314 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.
1000.314 1000.314 1000.414
- How to convert string to boolean in PHP?
- Convert List of Characters to String in Java
- Convert a String to a List of Characters in Java
- PHP | Find the number of sub-string occurrences
- How to convert array to SimpleXML in PHP
- How to convert timestamp to time ago in PHP ?
- Convert a column to row name/index in Pandas
- Convert an object to associative array in PHP
- How to convert a PDF document to a preview image in PHP?
- How to convert an image to base64 encoding in PHP?
- Convert an image into Blur using HTML/CSS
- Convert HashSet to TreeSet in Java
- Convert timestamp to readable date/time in PHP
- Convert byte array to File using Java
- Program to convert Byte Array to Writer in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.