While programming in PHP, there might be a situation where you have to convert a string into an integer. But we know that PHP does not need or support explicit type definition while declaring a variable. So you have to force a string to be converted into an integer.
We will look at the various ways by which a string can be converted into an integer in PHP in this article.
- Using number_format()
- Using Type Casting settype()
- Using intval() and floatval()
- By Performing Mathematical Operations
Adding Number "1" to String
Multiplying String with Number "0"
Why Do We Need to Convert String to Integer in PHP?
Sometimes you may need to convert a string into an integer, float or any other data type. This is because the data or values you are working on might be of a different data type. You might have an integer in a string format from the user. So you have to convert this string into a number for performing operations in the program.
Using number_format() Function
The number_format() function is an in-built method in PHP that is used for converting a string with grouped thousands. It takes 4 parameters:
string number_format ( $number, $decimals, $decimalpoint, $sep )
- $number - This is the number to be formatted. If you don't set any other parameters, this number will be formatted without any decimals
- $decimals - This is the optional parameter that is used for specifying the decimals
- $decimalpoint - This specifies the string used for mentioning the decimal point
- $sep - This is a string specifying the thousands separator
If the method successfully converts the string, it returns the formatted number. On failure, it returns an E_WARNING error message.
Syntax
number_format($string); // string into integer
number_format($string,2); // string into float
Code Example:
<?php
// Python program to convert string to number
$string = '25';
// Check if string in numeric
if (is_numeric($string)){
// Convert string to integer using number_format function
echo 'Integer Value: '.number_format($string);
// Convert string to float using number_format function
echo '<br>Float Value: '.number_format($string,2);
}else{
echo 'Not a numaric value';
}
?>
Output:
Integer Value: 25
Float Value: 25.00
Explanation:
In this program, an integer in the form of a string is assigned to a variable called $string. Then the is_numeric() checks the value of the variable and then formats it using the number_format() method. if the value is not numeric, the echo statement prints out the string “Not a numeric value”.
Using Type Casting settype()
The settype() method is used for setting the type of a variable and modify its existing type.
settype($variable_name, $type)
- $variable – This is the variable that needs to be converted
- $type – This is the type you want to convert the variable to
Syntax:
(int)$string; // string into integer
(float)$string; // string into float
(double)$string; // string into double
Code Example:
// Declare string value
$string = '25';
$stringf = '25.99';
$stringd = '25.99';
// Check if string in numaric
if (is_numeric($string)){
// Convert string to integer using int type cast
echo 'Integer Value: '.(int)$string;
}else{
echo 'Not a numaric value';
}
// Check if string in numaric
if (is_numeric($stringf)){
// Convert string to float using float type cast
echo '<br>Float Value: '.(float)$stringf;
}else{
echo 'Not a numaric value';
}
// Check if string in numaric
if (is_numeric($stringf)){
// Convert string to float using float type cast
echo '<br>Double Value: '.(double)$stringd;
}else{
echo 'Not a numaric value';
}
Output:
Integer Value: 25
Float Value: 25.99
Double Value: 25.99
Explanation:
In this code, you can observe that typecasting is used for converting the string into an integer. After evaluating whether the given variable has a string value or not using the is_numeric method, the line echo 'Integer Value: '.(int)$string; converts the string into an integer. Similarly, later on in the program, variables are converted to double and float, by mentioning the data type along with the variable name.
Using intval() and floatval() Function
The intval() function is used for returning the integer value of a variable.
int intval ( $var, $base )
It takes two parameters:
- $var – This is the variable that you want to convert into an integer
- $base – This optional parameter specifies the base for conversion. If $var has 0x as a prefix, 16 is taken as the base. If $var starts with 0, 8 is taken as the base
The floatval() method is used for obtaining the float value of a variable. It takes only one parameter $var, that is the value whose float value needs to be determined.
Syntax:
intval($string); // Convert string to integer
floatval($string); // Convert string to float
Code Example:
// Declare string value
$string = '25.99';
// Check if string in numaric
if (is_numeric($string)){
// Convert string to integer using intval function
echo 'Integer Value: '.intval($string);
// Convert string to float using floatval function
echo '<br>Integer Value: '.floatval($string);
}else{
echo 'Not a numaric value';
}
Output:
Integer Value: 25
Integer Value: 25.99
Explanation:
You can see that in the example, the intval() converts the value stored in the $string variable into an integer. Later the same value is converted into a float using the floatval() method.
Performing Mathematical Operations on String Value
Here, no in-built functions are used for a string to be converted to an integer.
By Adding Number "0" to String
Syntax:
($string + 0);
Code Example:
<?php
$string = '25.23';
// Check if string in numaric
if (is_numeric($string)){
// Convert string to integer by adding 0
echo 'Integer Value: ' . ($string + 0);
// Convert string to float by adding 0.00
echo '<br>Float Value: ' . ($string + 0.00);
}else{
echo 'Not a numaric value';
}
?>
Output:
Integer Value: 25.23
Float Value: 25.23
Explanation:
This is one of the easiest ways to convert a string into an integer. In this program, 0 is added to the string, which converts it into a number. By adding the string with 0.00 the value is converted into a float. This conversion is done by PHP automatically.
By Multiply Number "1" to String
Syntax:
($string * 1);
Code Example:
<?php
$string = '25.23';
// Check if string in numaric
if (is_numeric($string)){
// Convert string to integer by multiply by 1
echo 'Integer Value: ' . ($string * 1);
// Convert string to float by multiply by 1.00
echo '<br>Float Value: ' . ($string * 1.00);
}else{
echo 'Not a numaric value';
}
?>
Output:
Integer Value: 25.23
Float Value: 25.23
Explanation:
Another simple method for a string to be converted to an integer is by multiplying it by 1. So, the string 25.23 is multiplied by 1 to get an integer value.
Conclusion:
All these methods are applied when you get a number as a string but want to perform some mathematical operations on it. Although the intval(), float(), number_format() and type casting methods are very effective, adding 0 and multiplying 1 to a string is a very fast way to convert it into a number.