How do I convert a string to a number in PHP?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In PHP, converting a string to a number is a common task that you may encounter while handling data that comes from various sources such as user input, databases, or files. PHP is a loosely typed language, thus it provides straightforward ways to perform these conversions. Here we detail the methods you can use to convert a string into either an integer or a floating-point number, depending on the context and requirement.
Automatic Type Casting
PHP automatically converts a string to a number when needed. When a string is used in a numeric context, the conversion is performed automatically.
This behavior is effective when you know the string represents a straightforward number, but it can lead to unexpected results if the string includes non-numeric characters.
Explicit Type Casting
For more controlled conversion and to avoid ambiguity in your code, PHP supports explicit type casting. You can cast a string specifically to an integer or float using the following methods:
Another method for explicit casting uses the settype() function, which can modify the type of the variable directly:
Functions Specific to Type Conversion
intval()
Used extensively for converting a string to an integer.
floatval() and doubleval()
Both functions are used to obtain float value from a string.
Handling Non-numeric Characters
What happens when the string is "100abc20"? PHP behaves differently based on the function or conversion method:
- Automatic Type Casting: Reads up to the first non-numeric character and stops.
- Explicit Type Casting: Similar to automatic casting.
intval()and other explicit functions: Same as casting, they read until encountering a non-numeric character.
Number Format Conversions
PHP can handle differently formatted numbers (like currency or decimal commas) through helper functions like number_format() before conversion to ensure accuracy in type casting.
Utilizing Filters for Validation and Sanitization
PHP offers filter_var() for more robust data handling, allowing for validation and sanitization of formats before conversion:
The benefit of using filter_var() is the provision to cleanly handle untrusted data inputs, mitigating risks of malformed data impacting the application negatively.
Summary
The following table summarizes the conversion methods:
| Method | Use | Example | Result |
| Automatic Type Casting | Basic conversion | $result = "10" + 1; | 11 |
| Explicit Type Casting | Convert while controlling type | $int = (int)"100.99"; | 100 |
intval() | Convert string to integer | $int = intval("100.99"); | 100 |
floatval() | Convert string to float | $float = floatval("100.99"); | 100.99 |
filter_var() with FILTER_SANITIZE_NUMBER_FLOAT | Sanitize and convert to float | $float = filter_var("100.99", FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); | 100.99 |
The use of specific functions and type casts depends significantly on the context, such as the source of input data and desired robustness of type stability in your programs. Always consider validation and sanitization when handling data from non-trusted sources to prevent errors and injection attacks.
Handling numeric conversions properly ensures that the PHP applications you develop are more robust, predictable, and secure.

