Home >Backend Development >PHP Problem >What are the functions in php that convert values ​​to floating point types?

What are the functions in php that convert values ​​to floating point types?

青灯夜游
青灯夜游Original
2022-06-13 15:33:122560browse

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.

What are the functions in php that convert values ​​to floating point types?

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(&#39;content-type:text/html;charset=utf-8&#39;); 
$str="23.32uj";
$f1=floatval($str);
var_dump($f1);
$num=23;
$f2=floatval($num);
var_dump($f2);
?>

What are the functions in php that convert values ​​to floating point types?

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(&#39;content-type:text/html;charset=utf-8&#39;); 
$str="23.32uj";
settype($str,"float");
var_dump($str);
$num=23;
settype($num,"float");
var_dump($num);
?>

What are the functions in php that convert values ​​to floating point types?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What do php and jsp mean?Next article:What do php and jsp mean?