Signature
floatval(mixed $value): floatPurpose
floatval() returns the floating-point value of whatever you pass in. It applies the same rules as the (float) cast, but as a normal function call. That makes it handy as a callback, for example in array_map('floatval', $values), or whenever you prefer a named function to a cast.
The function never throws for ordinary scalar input. It reads what it can and quietly falls back to 0.0 when nothing usable is found. This leniency is convenient, but it also hides bad input, which is the main source of mistakes.
Signature
Parameters and defaults
$value is the only parameter and has no default. The manual describes it as accepting any scalar type. In practice you can also pass null and arrays, and both have defined results. Objects are the exception: floatval() should not be used on them. Doing so emits an E_WARNING and gives 1.0. Since PHP 8.0 that diagnostic is a warning; before then it was a notice.
Return value and failure cases
The return type is always float. There is no false or null failure result, so you cannot tell "the input was zero" from "the input was garbage" by looking at the return value alone.
- Integers become the equivalent float.
truebecomes1.0, andfalseandnullbecome0.0.- An empty array gives
0.0. A non-empty array gives1.0. - Strings are parsed from the left. A leading numeric prefix is used and the rest is ignored. If the string does not start with a number, the result is
0.0.
Example 1: how strings are read
The parser skips leading whitespace, then consumes a number. It accepts decimal points and exponent notation. It stops at the first character that cannot continue the number.
var_dump(floatval('122.34343The'));
var_dump(floatval('The122.34343'));
var_dump(floatval('1e3'));
var_dump(floatval(' 1.5'));float(122.34343) float(0) float(1000) float(1.5)
The first string keeps its numeric prefix. The second starts with a letter, so nothing is read and the result is zero. The exponent form 1e3 is understood and becomes 1000.
Example 2: non-string input
Booleans, null, integers and arrays all have fixed conversions. Arrays are checked only for emptiness; their contents are never examined.
var_dump(floatval(true));
var_dump(floatval(false));
var_dump(floatval(null));
var_dump(floatval(42));
var_dump(floatval([]));
var_dump(floatval([0]));float(1) float(0) float(0) float(42) float(0) float(1)
Notice that [0] yields 1, not 0. The array is non-empty, and that is the only thing that matters.
Example 3: summing loosely formatted input
A realistic use is tolerant parsing of user-supplied amounts where trailing text such as a currency code should be ignored.
$inputs = ['12.50', '3.25 USD', 'free', '1,5'];
$total = 0.0;
foreach ($inputs as $input) {
$total += floatval($input);
}
echo $total, "\n";16.75
The result is 16.75. The text free contributes zero. The European-style 1,5 is read only up to the comma, so it contributes 1, not 1.5. If you accept such input, normalize the separator yourself before converting.
Edge cases
- Partial numbers: Because trailing junk is ignored,
'12abc'becomes12.0. That may be wrong for strict input handling. - No leading digits: Text such as
'abc',''or'$5'yields0.0. A currency symbol in front of the number stops parsing immediately. - Leading dot and exponents: Forms such as
'.5'and'1e3'are valid float syntax and are read as numbers. - Hexadecimal text: A string like
'0x1A'is not parsed as hex. Only the leading0is read, so the result is0.0. - Precision: The result is a binary floating-point number. Values such as
0.1cannot be stored exactly, so compare floats with a tolerance rather than==, and use integer cents or an arbitrary-precision library for money. - Very large values: A number too large for a float cannot be represented exactly. Check results with
is_infinite()if you accept untrusted input.
Common mistakes
- Treating it as validation.
floatval('abc')andfloatval('0')both return0.0. If you need to know whether the input is really numeric, check withis_numeric()first, or usefilter_var($value, FILTER_VALIDATE_FLOAT), which reports failure withfalse. - Passing objects. The call warns and returns
1.0, which is almost never what you wanted. Convert an object to a scalar yourself first. - Assuming locale-style decimals. A comma is not a decimal separator for this conversion. Replace it before converting if your input uses one.
- Expecting array contents to be summed or read. Only emptiness is tested.
- Confusing it with rounding.
floatval()changes the type, not the precision. Useround()if you need a specific number of decimals.
Related functions
intval()converts to integer and truncates fractional parts.strval()converts to string.boolval()converts to boolean.settype()changes the type of a variable in place.is_numeric()andfilter_var()validate before you convert.
Recap
floatval() takes one mixed value and always returns a float. Strings are read from the left, with trailing text ignored and non-numeric starts giving 0.0. Booleans, null and arrays follow fixed rules, and objects cause a warning and 1.0. Use it for lenient conversion, and use a validator when invalid input must be detected.
Tested examples
var_dump(floatval('122.34343The'));
var_dump(floatval('The122.34343'));
var_dump(floatval('1e3'));
var_dump(floatval(' 1.5'));
float(122.34343) float(0) float(1000) float(1.5)
var_dump(floatval(true));
var_dump(floatval(false));
var_dump(floatval(null));
var_dump(floatval(42));
var_dump(floatval([]));
var_dump(floatval([0]));
float(1) float(0) float(0) float(42) float(0) float(1)
$inputs = ['12.50', '3.25 USD', 'free', '1,5'];
$total = 0.0;
foreach ($inputs as $input) {
$total += floatval($input);
}
echo $total, "\n";
16.75