Forcing conversion method: 1. Add the target type "(int)", "(bool)", "(float)", etc. enclosed in parentheses before the variable to be converted, and it will be converted For the corresponding type; 2. Use type conversion functions "intval (variable)", "floatval (variable)", "strval (variable)", etc.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Data type conversion refers to converting variables or values from Convert one data type to other data types. There are two methods of conversion, namely automatic type conversion and forced type conversion.
The following article will introduce to you the forced type conversion method of PHP.
There are two ways to force type conversion in PHP:
You can add parentheses before the variable to be converted. Target type (such as $var = (int)3.1415;
),
can use specific type conversion functions (such as intval(), floatval(), strval (), etc.) or settype() to convert the type.
1. Add the target type enclosed in parentheses before the variable to be converted.
The variable types allowed in the parentheses are as follows: Display:
(int), (integer): Convert to integer type;
(bool), (boolean): Convert to Boolean Type;
(float), (double), (real): converted to floating point type;
(string): converted to String type;
(array): converted to array type;
(object): converted to object type.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = '123.456abc'; echo '原变量 $str 的类型为:'.gettype($str)."<br>"; $int = (int)$str; echo '变量 $int 的类型为:' . gettype($int) . '<br>'; $float = (float)$str; echo '变量 $float 的类型为:' . gettype($float) . '<br>'; $bool = (bool)$str; echo '变量 $bool 的类型为:' . gettype($bool). '<br>'; $arr = (array)$str; echo '变量 $arr 的类型为:' . gettype($arr); ?>
2. Use specific type conversion functions
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(variable, "type"), used to convert the variable to the specified type.
Example 1:
<?php header('content-type:text/html;charset=utf-8'); $str = '123.456abc'; echo '原变量 $str 的类型为:'.gettype($str)."<br>"; $int = intval($str); echo '变量 $int 的类型为:'.gettype($int).'<br>'; $float = floatval($str); echo '变量 $float 的类型为:'.gettype($float).'<br>'; $bool = boolval($str); echo '变量 $bool 的类型为:'.gettype($bool); ?>
Example 2:
<?php header('content-type:text/html;charset=utf-8'); $str = '123.456abc'; echo '原变量的类型为:'.gettype($str)."<br>"; settype($str, 'integer'); echo '变量类型变为:'.gettype($str).'<br>'; settype($str, 'boolean'); echo '变量类型变为:'.gettype($str).'<br>'; settype($str, 'null'); echo '变量类型变为:'.gettype($str); ?>
Note: The settype() function will change the original variable itself; it returns TRUE when the setting is successful, and returns FALSE when it fails.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the methods of forced type conversion in php. For more information, please follow other related articles on the PHP Chinese website!