Home >Backend Development >PHP Problem >What are the functions in php that convert values to floating point types?
php has two functions to convert values to floating point types: 1. The floatval() function can obtain the floating point value of a variable. It is mainly used to convert variables to floating point types. The syntax "floatval() value)"; 2. The settype() function can set the variable to the specified type. When the second parameter is set to "float", the variable can be set to floating point type. The syntax is "settype($var,"float" )", this function will change the original variable, return TRUE if the setting is successful, and return FALSE if it fails.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php transfers the value There are two functions for floating point types:
floatval()
settype()
1. Use floatval() to convert the value to floating point type
floatval() is used to obtain the floating point value of the variable, mainly used to convert the value Convert to floating point type.
<?php header('content-type:text/html;charset=utf-8'); $str="23.32uj"; $f1=floatval($str); var_dump($f1); $num=23; $f2=floatval($num); var_dump($f2); ?>
2. Use settype() to convert the value to floating point type
settype($var,$ type)
function is used to set the variable $var to the specified type $type. Returns TRUE if the setting is successful and FALSE if it fails.
When the $type parameter of this function is set to "float", the value can be converted to floating point type.
Note: The settype() function will change the original variable.
<?php header('content-type:text/html;charset=utf-8'); $str="23.32uj"; settype($str,"float"); var_dump($str); $num=23; settype($num,"float"); var_dump($num); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the functions in php that convert values to floating point types?. For more information, please follow other related articles on the PHP Chinese website!