PHP Function Reference

PHP floatval() Function



The PHP floatval() function returns the float value of a variable.

Syntax

floatval(variable)

Parameters

variable Required. Specify the variable whose corresponding float value will be returned. Must be a scalar type.

Return Value

Returns the float value of the given variable. Empty arrays return 0, non-empty arrays return 1.

Strings will most likely return 0 although this depends on the leftmost characters of the string. If the string is numeric or leading numeric then it will resolve to the corresponding float value, otherwise it is converted to 0.

Note: If an object is passed to this function, it throws an E_NOTICE level error and returns 1.

Example:

The example below shows the usage of floatval() function.

<?php
$x = "1234.56789";
echo floatval($x)."\n";

$y = "1234.56789Hello";
echo floatval($y)."\n";

$z = "Hello1234.56789";
echo floatval($z)."\n";

$a = "12345E-3";
echo floatval($a)."\n";

$b = "12345E3";
echo floatval($b)."\n";
?>

The output of the above code will be:

1234.56789
1234.56789
0
12.345
12345000

Example:

Consider one more example which demonstrates the use of this function with arrays.

<?php
$arr1 = array();
echo floatval($arr1)."\n";

$arr2 = array(1, 2, 3);
echo floatval($arr2)."\n";
?>

The output of the above code will be:

0
1

❮ PHP Variable Handling Reference