PHP doubleval() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. 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 ValueDescription
floatThe 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.

PHP
<?php
var_dump(doubleval("3.14abc"));
var_dump(doubleval("1.5e3"));
var_dump(doubleval("abc"));
var_dump(doubleval(7));

How It Works

  1. "3. 14abc" starts with 3.14, so PHP uses that part and ignores the letters.
  2. "1. 5e3" is scientific notation for 1.5 times 10 to the power of 3, which is 1500.
  3. "abc" does not start with a number, so the result is 0.
  4. The integer 7 becomes the float 7. var_dump() prints floats as float(...).

Example 2 — Alias Proof and Tricky Strings

Check that doubleval() equals floatval(), then try a few strings that surprise beginners.

PHP
<?php
var_dump(doubleval("2.5") === floatval("2.5"));
var_dump(doubleval(" 12.5"));
var_dump(doubleval("1,234.5"));
var_dump(doubleval(true));

How It Works

  1. Both functions return the same float, so the strict comparison is true. That confirms doubleval() is an alias.
  2. Leading spaces are allowed, so " 12. 5" becomes 12.5.
  3. The comma stops the number, so "1,234. 5" is read only as 1.
  4. A boolean true converts to 1. 0. This is how PHP turns true into a number.

Example 3 — Adding Prices Stored as Text

When prices arrive as strings, convert each one to a float before adding them.

PHP
<?php
$prices = ["10.50", "4.25", "3"];
$total = 0.0;

foreach ($prices as $price) {
    $total += doubleval($price);
}

echo "Total: ", number_format($total, 2), PHP_EOL;

How It Works

  1. The prices are strings, which is common for data read from forms or files.
  2. $total starts as the float 0. 0.
  3. Each pass of the loop converts one string with doubleval() and adds it to $total.
  4. 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".

Next: empty()

empty() checks whether a variable is empty. It returns true when the variable does not exi.

empty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful