Home > Article > Backend Development > What are the PHP data type conversion functions?
php data type conversion function: 1. intval(), converted to integer type; 2. floatval(), converted to floating point type; 3. boolval(), converted to Boolean type; 4. strval( ), converted into a string type; 5. settype(), can be converted into a user-specified data type.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php data type conversion Function
Specific conversion functions intval(), floatval(), boolval(), strval(), settype(), the functions of these functions are as follows:
intval(): used to get the integer value of the variable;
floatval(): used to get the floating point value of the variable;
boolval(): used to get the Boolean value of the variable;
strval(): used to get the string value of the variable;
settype(): used to set the type of variables.
Example 1:
<?php $str = '123.456abc'; $int = intval($str); echo '变量 $int 的类型为:'.gettype($int).'<br>'; $float = floatval($str); echo '变量 $float 的类型为:'.gettype($float).'<br>'; $string = strval($str); echo '变量 $string 的类型为:'.gettype($string); ?>
The running results are as follows:
变量 $int 的类型为:integer 变量 $float 的类型为:double 变量 $string 的类型为:string
intval(), floatval(), boolval() When the strval() function converts types, it will not change the type of the converted variable itself. Instead, it will assign the converted new type of data to the new variable, and the type and value of the original variable will remain unchanged.
Example 2:
<?php $str = (string)'123abc'; $bool = (bool)true; settype($str, 'integer'); settype($bool, 'string'); echo '$str 的类型为:'.gettype($str).'<br> $bool 的类型为:'.gettype($bool); ?>
The running results are as follows:
$str 的类型为:integer $bool 的类型为:string
settype() function will change the type of the variable itself, the syntax format is:
settype(mixed &$var, string $type)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the PHP data type conversion functions?. For more information, please follow other related articles on the PHP Chinese website!