php8.5
Home/ Manual/ var / functions/ floatval

floatval

PHP function Edit on GitHub ✎

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

Get float value of a variable

Description

floatval(mixed $value): float

Gets the float value of value.

Parameters

value

May be any scalar type. floatval() should not be used on objects, as doing so will emit an E_WARNING level error and return 1.

Return Values

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. The common rules of float casting apply.

Changelog

VersionDescription
8.0.0The error level when converting from object was changed from E_NOTICE to E_WARNING.

Examples

floatval() Example

php
<?php
$var = '122.34343The';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343
?>

floatval() non-numeric leftmost characters Example

php
<?php
$var = 'The122.34343';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 0
?>

See Also

Source: reference/var/functions/floatval.xml · from the official PHP manual (php/doc-en)