doubleval()
Learn PHP doubleval(), which converts a value to a float and is an exact alias of floatval(). Covers syntax, return values, and string parsing rules.
Introduction
The doubleval() function converts a value to a float (a number with a decimal part). It is an exact alias of floatval() — both functions share the same implementation. The name exists because PHP historically called the floating-point type "double", and in PHP float and double are the same type.
You reach for doubleval() (or floatval()) whenever you have a value of an unknown or mixed type — typically a string from form input, a query string, or a file — and you need a reliable number to do arithmetic with.
Syntax
doubleval(mixed $value): float$value is the value to convert. It may be a string, integer, boolean, or other scalar. The function returns the float value of $value, or 0.0 when the value cannot be interpreted as a number.
Return values
How doubleval() treats a value depends on its type:
- Numeric string — parsed up to the first non-numeric character (
"3.14"→3.14). - Integer / float — returned as a float (
42→42.0internally). - Boolean —
true→1.0,false→0.0. null—0.0.- Non-numeric string —
0.0. - Array —
1.0for a non-empty array,0.0for an empty one (with a warning). Avoid passing arrays.
Basic example
Note how the output reads 42 and 1, not 42.0 and 1.0. The values are floats, but echo prints a float with no fractional part without a trailing .0. To see the float type explicitly, use var_dump():
<?php
var_dump(doubleval(42)); // float(42)
var_dump(doubleval(true)); // float(1)
var_dump(doubleval("3.14")); // float(3.14)
?>Parsing strings with leading numbers
doubleval() reads the leading numeric portion of a string and stops at the first character that cannot be part of a number. Whitespace at the start is skipped, and scientific notation is understood.
<?php
echo doubleval("12.5kg") . "\n"; // 12.5 (stops at "k")
echo doubleval(" 7.0 ") . "\n"; // 7 (leading spaces ignored)
echo doubleval("1.2e3") . "\n"; // 1200 (scientific notation)
echo doubleval("abc12") . "\n"; // 0 (no leading number)
?>This makes it convenient for pulling a number out of a unit-tagged string, but it also means a malformed value silently becomes 0.0 instead of raising an error — validate first if that distinction matters.
doubleval() vs. (float) cast
For scalar values, doubleval($x) and (float) $x produce the same result, so the cast is usually preferred in modern code. doubleval() is still handy as a callback, for example with array_map():
<?php
$inputs = ["1.5", "2", "3.75kg"];
$floats = array_map('doubleval', $inputs);
print_r($floats);
// Array ( [0] => 1.5 [1] => 2 [2] => 3.75 )
?>doubleval() vs. floatval()
There is no functional difference — doubleval() is a defined alias of floatval(). Modern code should prefer floatval() because the name is clearer and matches the float type name used everywhere else in PHP.
Conclusion
doubleval() converts any scalar value to a float, parsing the leading numeric part of strings and falling back to 0.0 when no number is found. It is identical to floatval(), which is the preferred name today. For converting to other types, see intval() and settype().