HOWTO · PHP
How to Convert a String to a Number in PHP
Convert numeric strings to integers or floats in PHP with explicit casts, intval(), floatval(), or settype().
On this page
To convert a numeric string to an integer in PHP, cast it with (int) or call intval(). To convert it to a floating-point number, use (float) or floatval(). These casts and functions produce a converted value while leaving the source string available. Use settype() when you want to change the type of an existing variable in place. The right choice depends on whether you want a concise expression, a named conversion function, or a change to the variable itself.
Use Explicit Type Casting to Convert a String to a Number in PHP
An explicit cast creates a converted value using the type in parentheses. Use (int) for an integer or (float) for a floating-point value. This is a direct option when the desired type is clear at the point where you use the value. The following standalone example converts one numeric string of each kind:
<?php
$integer = (int) "10";
$decimal = (float) "8.1";
echo $integer, "\n", $decimal, "\n";
10
8.1
The cast expression can be assigned to a new variable, as shown, or used wherever an expression is accepted. Assigning it to $integer or $decimal leaves the original literal or source variable unchanged; you can also reuse the same variable name if replacing its value is intentional. Pick (int) when the result should be an integer and (float) when the fractional part matters. These examples use numeric strings and do not describe how casts handle other input formats.
Use intval() Function to Convert a String to a Number in PHP
The intval() function returns an integer value. For a floating-point value, use floatval(). These named functions make the intended conversion explicit in an expression and can be useful when that reads more clearly than a cast in your code. Both return converted values, so storing a result in another variable keeps the original string available:
<?php
$integer = intval("53");
$decimal = floatval("25.3");
echo $integer, "\n", $decimal, "\n";
53
25.3
Choose intval() for integer conversion and floatval() for floating-point conversion. The choice between a cast and its corresponding function is mainly about which form communicates your intent best; both examples here convert numeric strings to the requested type. Neither assignment replaces the source string unless you deliberately assign the returned value back to the same variable. This example makes no claim about parsing strings that contain other characters.
Use settype() Function to Convert a String to a Number in PHP
The settype() function takes a variable and a target type name. It changes that variable in place rather than returning a separate converted value. Use the target type integer to convert a numeric string to an integer:
<?php
$value = "45";
settype($value, "integer");
echo $value, "\n";
45
After the call, $value holds the converted value; the string previously assigned to that variable is no longer available through $value. If you need both forms, keep the string in one variable and pass a different variable to settype(), or use a cast or conversion function and assign its result separately. Use settype() when changing the existing variable is useful, and use (int), (float), intval(), or floatval() when you want a converted value that can be assigned or used without changing the source variable. The example is limited to an integer conversion from the shown numeric string.