W3docs

floatval()

The floatval() function is a built-in function in PHP that converts a variable to a float (floating-point) number. It is similar to the doubleval() function,

Introduction

The floatval() function is a built-in PHP function that returns the float (floating-point) value of a variable. You reach for it whenever you have a value of an uncertain type — typically a string coming from a form, a query string, a CSV file, or a JSON payload — and you want to be sure you are working with a number you can do arithmetic on.

floatval() is an alias of doubleval(). PHP has no separate double type — float and double are the same thing — so the two functions are interchangeable; floatval() is the spelling you should prefer in new code.

This page covers the syntax, how the function parses different inputs (including the surprising-at-first rule for strings), how it compares to the (float) cast, and the gotchas worth knowing.

Syntax

float floatval(mixed $value)

It takes a single argument, $value, and returns its float value. The argument should be a scalar (string, int, float, or bool); passing an array returns 1.0 for a non-empty array and 0.0 for an empty one, and passing an object raises an error.

Example Usage

The example below converts several different types and prints the result. Note that floatval() returns a float, but when PHP prints a whole-number float with echo it omits the trailing .0 — so 42.0 displays as 42.

php— editable, runs on the server

Here $var1 is a numeric string, $var2 is an integer, $var3 is a boolean, and $var4 is a string with no leading number. Each is converted to a float. To confirm the result really is a float (rather than rely on what echo shows), wrap a value in var_dump(): var_dump(floatval(42)); prints float(42).

How strings are parsed

This is the part that catches people out. When given a string, floatval() reads the leading numeric portion and stops at the first character that can't be part of a number. It does not require the whole string to be numeric.

<?php
echo floatval("12.5abc") . "\n"; // 12.5  (stops at "a")
echo floatval("  7.0kg")  . "\n"; // 7     (leading whitespace is skipped)
echo floatval("1.0e3")    . "\n"; // 1000  (scientific notation is understood)
echo floatval("$199.99")  . "\n"; // 0     (starts with "$", no leading number)
echo floatval("0x1A")     . "\n"; // 0     (hex strings are NOT parsed)
echo floatval("")         . "\n"; // 0     (empty string)
?>

The key rules:

  • Leading whitespace is ignored; the scan begins at the first non-space character.
  • Scientific notation (1.0e3) and a leading sign (-3.5, +2) are recognized.
  • Parsing stops at the first non-numeric character, and anything after it is discarded.
  • If there is no leading number at all, the result is 0.0. That means a currency string like "$199.99" yields 0, not 199.99 — strip the symbol first.
  • Hexadecimal strings such as "0x1A" are treated as the number 0, because the scan stops at the x.

floatval() vs. the (float) cast

floatval($x) and (float) $x produce the same result for scalar inputs, so for a single value the cast is shorter:

<?php
$price = "49.95 USD";
echo floatval($price) . "\n"; // 49.95
echo (float) $price   . "\n"; // 49.95
?>

The advantage of floatval() is that it is a callable. You can pass it directly to higher-order functions where a cast keyword can't go:

<?php
$inputs = ["1.5", "2.25", "x", "3"];
$floats = array_map('floatval', $inputs);
print_r($floats);
// Array ( [0] => 1.5 [1] => 2.25 [2] => 0 [3] => 3 )
?>

If you need strict validation — rejecting anything that isn't a clean number instead of silently coercing — use is_numeric() or filter_var($x, FILTER_VALIDATE_FLOAT) before converting, since floatval() never reports failure.

When to use it

  • Normalizing user input ($_POST, $_GET) or CSV/text data before doing math.
  • As a callback for array_map(), usort() comparisons, and similar.
  • Converting a value whose type you don't control into a guaranteed float.

If you want an integer instead, use intval(); to change a variable's type in place use settype(); to check a type use gettype() or is_float(). For an overview of how PHP handles types, see PHP Data Types.

Conclusion

floatval() returns the float value of any scalar and never throws on bad input — it falls back to 0.0 instead. That makes it convenient, but it also means you should validate first when a missing or malformed number must be treated as an error. Remember the leading-numeric-string rule ("12.5abc" becomes 12.5, "$199.99" becomes 0), and reach for floatval() over a cast whenever you need to pass conversion as a callback.

Practice

Practice
What is the function and usage of floatval in PHP?
What is the function and usage of floatval in PHP?
Was this page helpful?