Signature
doubleval(mixed $value): floatPurpose
doubleval() converts a value to a floating-point number. It is an alias of floatval(), so the two names behave identically. In PHP, float and double mean the same type, and the alias is a holdover from that older terminology. New code usually uses floatval() or a (float) cast, but you will still meet doubleval() in older codebases.
Signature
The signature is that of floatval(), because doubleval() is only another name for it.
Parameters and defaults
$valueis the value to convert. It is required and has no default. Any scalar type can be passed, and strings are the most common input.
Return value and failure cases
The function always returns a float. It does not return false and does not throw for ordinary scalar input. When a string cannot be read as a number, the result is 0.0. This means you cannot tell "0" from "abc" by looking at the result alone.
For strings, PHP reads the longest numeric prefix at the start of the text and ignores the rest. Leading whitespace is allowed. Exponent notation such as 1e3 is understood. Text that does not begin with a number produces 0.0.
Example 1: strings
var_dump(doubleval("12.5"));
var_dump(doubleval("3.14abc"));
var_dump(doubleval("abc"));
var_dump(doubleval("1e3"));float(12.5) float(3.14) float(0) float(1000)
The trailing abc after 3.14 is simply ignored. A string with no numeric prefix gives zero. var_dump() prints whole-number floats without a decimal point, so 1000.0 appears as float(1000).
Example 2: other scalar types
var_dump(doubleval(true));
var_dump(doubleval(null));
var_dump(doubleval(42));float(1) float(0) float(42)
true becomes 1.0, null becomes 0.0, and an integer is widened to the equivalent float.
Example 3: summing loose input
$inputs = ["10.50", "2.25", "3abc"];
$total = 0.0;
foreach ($inputs as $input) {
$total += doubleval($input);
}
echo json_encode(["total" => $total]);{"total":15.75}Each string is converted, with "3abc" contributing 3.0. This is convenient but permissive. If bad input should be rejected rather than silently accepted, validate it first.
Edge cases
- Decimal commas: only the dot is a decimal separator.
doubleval("1,5")reads the1and stops at the comma, giving1.0. - Hexadecimal and other bases:
"0x1A"is not interpreted as hex. The conversion reads the leading0and returns0.0. - Overflow: a number too large for a float, such as
"1e999", becomesINF. - Empty string:
""gives0.0. - Precision: the result is a binary float. Values like
0.1cannot be stored exactly, so compare floats with a tolerance and round money values carefully. - Arrays and objects: these are not meaningful inputs for this function. Check the type before calling it instead of relying on whatever result you get.
Common mistakes
- Treating the result as validation. A result of
0.0may mean the input was"0"or text with no number. Useis_numeric()orfilter_var()withFILTER_VALIDATE_FLOATwhen you need to know whether the input was valid. - Accepting partial numbers by accident.
"12px"becomes12.0without any warning, which can hide data-entry errors. - Assuming locale formats work. European-style input like
"1.234,56"is not parsed as you might expect. Normalize the string first. - Comparing floats with
===after arithmetic. Small representation errors can make mathematically equal values differ. - Using it where an integer is needed. If the value must be whole, use
intval()or validate it as an integer, becausedoubleval()always returns a float.
Related functions
floatval(), the primary name for this behaviorintval()for integer conversionis_numeric()to check whether a value is a number or numeric stringis_float()to test the type of a valuefilter_var()for stricter validationround()for controlling decimal places after conversion
Recap
doubleval() is an alias of floatval() that takes any value and returns a float. Numeric strings are read from the start, extra trailing text is ignored, and text with no leading number becomes 0.0. It never reports invalid input, so validate separately when correctness matters. Prefer the floatval() name or a (float) cast in new code for consistency.
Tested examples
var_dump(doubleval("12.5"));
var_dump(doubleval("3.14abc"));
var_dump(doubleval("abc"));
var_dump(doubleval("1e3"));float(12.5) float(3.14) float(0) float(1000)
var_dump(doubleval(true));
var_dump(doubleval(null));
var_dump(doubleval(42));float(1) float(0) float(42)
$inputs = ["10.50", "2.25", "3abc"];
$total = 0.0;
foreach ($inputs as $input) {
$total += doubleval($input);
}
echo json_encode(["total" => $total]);{"total":15.75}