The floatval() function returns the value of a passed expression as a float type. Numbers passed to the function return the same number as a float. Strings expression return a 0 unless the leading part of the string can be parsed as a number. If so, the value of the number will be returned. A Boolean value of true returns 1, whereas a value of false returns 0. Empty arrays passed to the function will return 0, but non-empty arrays return 1. Passing an object will generate an error and return 1.


float floatval( mixed $expr )


Expression for which the float value should be returned. The passed expression can be any type except object.

Return Value

The passed expression as a float.


1. Evaluate Various Expression Types as float

This example passes literals of various types to floatval() and displays the returned value.

echo 'floatval(0):           ' . floatval(0)           . "\n";
echo 'floatval(1.23):        ' . floatval(1.23)        . "\n";
echo 'floatval(""):          ' . floatval("")          . "\n";
echo 'floatval("abc"):       ' . floatval("abc")       . "\n";
echo 'floatval("12abc"):     ' . floatval("12abc")     . "\n";
echo 'floatval("12.345abc"): ' . floatval("12.345abc") . "\n";
echo 'floatval("abc12.345"): ' . floatval("abc12.345") . "\n";
echo 'floatval(true):        ' . floatval(true)        . "\n";
echo 'floatval(false):       ' . floatval(false)       . "\n";
echo 'floatval([]):          ' . floatval([])          . "\n";
echo 'floatval([2,3]):       ' . floatval([2,3])       . "\n";

The output of the program above produces the output below. Passed numbers return the expected values. Passed strings return zero unless the leading part of the string is a valid numeric. Boolean true is returned as 1 and Boolean false is returned as 0. Empty arrays are returned as 0 and non-empty arrays are returned as 1.

floatval(0):           0
floatval(1.23):        1.23
floatval(""):          0
floatval("abc"):       0
floatval("12abc"):     12
floatval("12.345abc"): 12.345
floatval("abc12.345"): 0
floatval(true):        1
floatval(false):       0
floatval([]):          0
floatval([2,3]):       1

2. More String to Number Conversions

This example shows some more string to float conversions. Hyphens, dollar signs, commas, exponential, octal, and hexadecimal notation are examined.

echo 'floatval("-12.6abc"):   ' . floatval("-12.6abc")   . "\n";
echo 'floatval("-12.6e3abc"): ' . floatval("-12.6e3abc") . "\n";
echo 'floatval("017"):        ' . floatval("017")        . "\n";
echo 'floatval(017):          ' . floatval(017)          . "\n";
echo 'floatval("0x17"):       ' . floatval("0x17")       . "\n";
echo 'floatval(0x17):         ' . floatval(0x17)         . "\n";
echo 'floatval("$123.45"):    ' . floatval("$123.45")    . "\n";
echo 'floatval("-123,456"):   ' . floatval("-123,456")   . "\n";

The output of the example above follows. A leading hyphen is correctly interpreted as a negative number. Trailing exponential notation (e.g. “e3”) is also parsed correctly even where there is trailing garbage.

Octal and hexadecimal notation is not considered when converting strings to numbers with floatval(). A leading “0” in a string (e.g. “017”) is just treated as a 0 and does not flag the value as an octal number. A leading “0x” (e.g. “0x17”) does not indicate a hexadecimal number. The “x” is considered a letter which causes the number parsing to end, thus returning a 0.

Finally, embedded dollar signs and commas are interpreted as garbage just like any other letter.

floatval("-12.6abc"):   -12.6
floatval("-12.6e3abc"): -12600
floatval("017"):        17
floatval(017):          15
floatval("0x17"):       0
floatval(0x17):         23
floatval("$123.45"):    0
floatval("-123,456"):   -123

3. Evaluate an Object as a float

Here we attempt to pass an object to floatval().

echo 'floatval(new stdClass): ' . floatval(new stdClass) . "\n";

The output is shown below. This generates an error message but returns the value 1.

PHP Notice:  Object of class stdClass could not be converted to double in /home/charlie/php/floatval2.php on line 2
floatval(new stdClass): 1

See Also