php

How to Convert String to a Number in PHP?

Converting a string to a number is a common task in PHP programming, especially when working with user inputs or calculating data. PHP provides several methods for converting a string to a number, including typecasting and built-in functions. However, choosing the right method for your specific use case can be crucial to ensure that the conversion is accurate and safe.

In this article, various methods for converting a string into a number in PHP will be presented.

Convert String to a Number

The following are several methods available for converting a string to a number in PHP:

1: Using Arithmetic Operators

PHP supports arithmetic operators such as +, -, *, /, and % that can convert a string to a number implicitly. This means that if you use an arithmetic operator on a string that starts with a numeric character, PHP will automatically treat the string as a number and perform the corresponding operation.

For example, suppose you have a variable $Str that contains the string “10” and you can convert it to an integer by adding it to 0:

<?php
$Str = "10";
$Int = $Str + 0;
echo $Int;
?>

Output

2: Using Typecasting

Typecasting is a way to explicitly convert a variable from one data type to another and you can use the (int) or (integer) casting operator to convert a string to an integer, or the (float) or (double) casting operator to convert a string to a floating-point number.

For example, if we have a string variable $Str that contains the string “10”., we can convert it to an integer using typecasting as follows:

<?php
$Str = "10";
$Int = (int) $Str;
echo $Int;
?>

Output

You can replace int with a float in the above example to convert the string to a floating number.

<?php
$Str = "3.69";
$Float = (float) $Str;
echo $Float;
?>

Output

3: Using Built-in Functions

3: Using Built-in Functions
PHP provides several built-in functions that can convert a string to a number, such as intval() and floatval(), and number format().

i: intval()

The intval() function converts a string to an integer as it takes an optional second argument that specifies the base of the number system (e.g., 2 for binary, 8 for octal, 10 for decimal, while16 for hexadecimal).

<?php
$Str = "10";
$Int = intval($Str);
echo $Int;
?>

Output

ii: floatval()
For floating-point numbers, you can use the floatval() instead to into to convert a string to a floating-point number.

<?php
$Str = "3.69";
$float_num = floatval($Str);
echo $float_num;
?>

Output

iii: number_format()
For using the number_format() function, you have to pass a string to number_format(), it will be converted to a number first.

<?php
$Str = "42";
$formatted = number_format($Str);
echo $formatted;
?>

Output

Conclusion

Converting a string to a number is an essential task in PHP programming. We have discussed different methods for accomplishing this task, including arithmetic operators, typecasting, and built-in functions. By understanding these methods, you can ensure that the conversion is accurate and safe for your PHP code.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.