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. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.
There are many techniques to convert strings into numbers in PHP, some of them are described below:
Table of Content
We will explore all of the mentioned approaches with the help of suitable examples.
Using the number_format() Function
The number_format() function is an inbuilt function in PHP that is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure.
Example: This example illustrates the transformation of the string into a number in PHP & formats the number with a specific group pattern.
<?php
$num = "1000.314";
// Convert string in number
// without decimal point parameter
echo number_format($num), "\n";
// Convert string in number
// with decimal Point parameter
echo number_format($num, 2);
?>
Output
1,000 1,000.31
Using Type Casting
Type Casting can directly convert a string into a float, double, or integer primitive type. This is the best way to convert a string into a number without any function.
Example: This example illustrates converting a string into a number in PHP using Type Casting.
<?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 (float) $num;
?>
Output
1000 1000.314 1000.314
Using the intval() and the floatval() Functions
The intval() and floatval() functions can also be used to convert the string into its corresponding integer and float values respectively.
Example: This example illustrates converting a string into a number in PHP using the intval() and the floatval() Functions.
<?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);
?>
Output
1000 1000.314
Using Mathematical Operations
In this approach, we will be adding 0 or by performing mathematical operations. The string number can also be converted into an integer or float by adding 0 to the string. In PHP, performing mathematical operations, the string is converted to an integer or float implicitly.
Example: This example illustrates converting a string into a number in PHP using Mathematical Operations.
<?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;
?>
Output
1000.314 1000.314 1000.414
Using the filter_var() Function:
The filter_var() function in PHP can be used to convert a string to a number by using the FILTER_SANITIZE_NUMBER_FLOAT or FILTER_SANITIZE_NUMBER_INT filter. This function is versatile and can handle various types of sanitization and validation.
Example: This example illustrates converting a string into a number in PHP using the filter_var() function.
<?php
$string = "123.45";
// Converting string to float
$floatNumber = filter_var($string, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
echo $floatNumber; // Outputs: 123.45
// Converting string to integer
$intNumber = filter_var($string, FILTER_SANITIZE_NUMBER_INT);
echo $intNumber; // Outputs: 12345
?>
Output
123.4512345
Using the settype() Function
The settype() function is used to convert a variable to a specified type. This function can change the type of a variable to integer or float, among other types. It is another effective way to convert a string to a number in PHP.
Example: This example illustrates converting a string into a number in PHP using the settype() function.
<?php
$num = "1000.314";
settype($num, "integer");
echo $num, "\n";
$num = "1000.314";
settype($num, "float");
echo $num, "\n";
?>
Output
1000 1000.314
Using sscanf Function
The sscanf function in PHP can be used to parse input from a string according to a specified format. This function can be leveraged to convert a string into a number, such as an integer or a float.
<?php
$string = "12345.6789";
sscanf($string, "%f", $number);
echo $number;
$string_int = "98765";
sscanf($string_int, "%d", $number_int);
echo $number_int;
?>
Output
12345.678998765
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.