Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

floatval()

Convert a value to floating point.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

floatval(mixed $value): float

Convert a value to floating point. Strings are parsed from a numeric prefix, so a measurement with trailing units can yield a number while a leading unit yields zero. Conversion alone cannot verify that the whole input is a valid number. Both null and an empty array convert to 0.0, and a nonempty array converts to 1.0. In PHP 8 a plain stdClass converts to 1.0 with E_WARNING.

Signature

floatval(mixed $value): float

Example output

[[2.5,0.0,-35.0,0.0,0.0,1.0,1.0],[true]]

Examples

<?php
$warnings = [];
set_error_handler(function($level) use (&$warnings) { $warnings[] = $level === E_WARNING; return true; });
$values = array_map("floatval", ["2.5kg","kg2.5","-3.5e1",null,[],[0],new stdClass()]);
restore_error_handler();
echo json_encode([$values,$warnings],JSON_PRESERVE_ZERO_FRACTION);

Result

[[2.5,0.0,-35.0,0.0,0.0,1.0,1.0],[true]]

Sources