In PHP, doubleval() is an alias of floatval(). It converts a value to a floating-point number and returns it.
The two functions are identical, so every example for floatval() works the same with doubleval(). The name comes from the C language, where a floating-point number is called a double. For new code, prefer the clearer name floatval(), or use the (float) cast.
Syntax
Call doubleval() with the values you need. Start with this line.
PHP
$number = doubleval($value);
Call
What it does
doubleval("3.14abc")
Returns 3.14, because PHP reads the number at the start of the string.
doubleval("abc")
Returns 0.0, because the string does not start with a number.
doubleval(7)
Returns 7.0, the integer converted to a float.
doubleval() is an alias, so it behaves exactly like floatval(). The preferred form is floatval(), because "float" is the name PHP uses everywhere else, such as in is_float() and gettype().
Library declarations
PHP
// doubleval() is an alias of floatval()
doubleval(mixed $value): float
Parameters
Parameter
Description
value
Required. The value to convert to a float. Strings are read from the start, up to the first character that cannot be part of a number.
Return Value
Return Value
Description
float
The float value of the input. Strings that do not start with a number give 0.0. Empty arrays give 0.0 and non-empty arrays give 1.0.
PHP Examples
Example 1 — Converting Strings and Integers
doubleval() reads a number from the start of a string and returns it as a float.
The prices are strings, which is common for data read from forms or files.
$total starts as the float 0. 0.
Each pass of the loop converts one string with doubleval() and adds it to $total.
number_format($total, 2) prints the sum with two decimal places. In new code, write floatval() instead of doubleval().
Frequently Asked Questions
doubleval() is an alias of floatval(). It converts a value to a float and behaves exactly the same way.
There is no difference in behavior. doubleval() is the older C-style name, and floatval() is the preferred name used by the PHP manual and by modern code.
No. It is still available in PHP 8 and works the same as floatval(). Still, floatval() or the (float) cast makes your code easier to read.
It returns 0.0 (shown as float(0)). PHP reads the number at the start of the string, so a string like "abc" has no number to read.
Yes. PHP has only one floating-point type, stored as a 64-bit double-precision number. The words float, double and real all refer to this same type, though gettype() reports it as "double".