Types7 min readExamples run on PHP 8.5.10

doubleval()

Learn how doubleval(), the alias of floatval(), converts values to float in PHP 8.4, including how it handles partial numeric strings and other edge cases.

Signature

doubleval(mixed $value): float

Purpose

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

  • $value is 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

Example
<?php
var_dump(doubleval("12.5"));
var_dump(doubleval("3.14abc"));
var_dump(doubleval("abc"));
var_dump(doubleval("1e3"));
Output
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

Example
<?php
var_dump(doubleval(true));
var_dump(doubleval(null));
var_dump(doubleval(42));
Output
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

Example
<?php
$inputs = ["10.50", "2.25", "3abc"];
$total = 0.0;
foreach ($inputs as $input) {
    $total += doubleval($input);
}
echo json_encode(["total" => $total]);
Output
{"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 the 1 and stops at the comma, giving 1.0.
  • Hexadecimal and other bases: "0x1A" is not interpreted as hex. The conversion reads the leading 0 and returns 0.0.
  • Overflow: a number too large for a float, such as "1e999", becomes INF.
  • Empty string: "" gives 0.0.
  • Precision: the result is a binary float. Values like 0.1 cannot 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

  1. Treating the result as validation. A result of 0.0 may mean the input was "0" or text with no number. Use is_numeric() or filter_var() with FILTER_VALIDATE_FLOAT when you need to know whether the input was valid.
  2. Accepting partial numbers by accident. "12px" becomes 12.0 without any warning, which can hide data-entry errors.
  3. Assuming locale formats work. European-style input like "1.234,56" is not parsed as you might expect. Normalize the string first.
  4. Comparing floats with === after arithmetic. Small representation errors can make mathematically equal values differ.
  5. Using it where an integer is needed. If the value must be whole, use intval() or validate it as an integer, because doubleval() always returns a float.
  • floatval(), the primary name for this behavior
  • intval() for integer conversion
  • is_numeric() to check whether a value is a number or numeric string
  • is_float() to test the type of a value
  • filter_var() for stricter validation
  • round() 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

Converting strings with doubleval()
<?php
var_dump(doubleval("12.5"));
var_dump(doubleval("3.14abc"));
var_dump(doubleval("abc"));
var_dump(doubleval("1e3"));
Output
float(12.5)
float(3.14)
float(0)
float(1000)
Converting other scalar types
<?php
var_dump(doubleval(true));
var_dump(doubleval(null));
var_dump(doubleval(42));
Output
float(1)
float(0)
float(42)
Summing loosely formatted input
<?php
$inputs = ["10.50", "2.25", "3abc"];
$total = 0.0;
foreach ($inputs as $input) {
    $total += doubleval($input);
}
echo json_encode(["total" => $total]);
Output
{"total":15.75}