The floatval Function
The floatval
function converts the passed value
to a floating-point number (float). As a parameter,
it accepts a variable of any type. If conversion
is impossible, the function will return 0
.
Syntax
floatval(mixed $value);
Example
Convert a string with a number to float:
<?php
$res = floatval('12.34');
var_dump($res);
?>
Code execution result:
12.34
Example
Let's try to convert a string with text:
<?php
$res = floatval('hello');
var_dump($res);
?>
Code execution result:
0
Example
Convert an integer to float:
<?php
$res = floatval(123);
var_dump($res);
?>
Code execution result:
123