Home >Backend Development >PHP Problem >How to force data type conversion in php
Forced conversion method: 1. Add the target type enclosed in parentheses ("(int)", "(bool)", "(float)", etc.) before the variable to be converted; 2. Use type conversion functions intval(), floatval(), boolval(), strval(), settype().
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP forced type conversion
Coercive type conversion in PHP is very similar to other languages. You can add the target type enclosed in parentheses before the variable to be converted (for example, $var = (int )3.1415;
), you can also use specific type conversion functions (such as intval()
, floatval()
, strval()
, etc.) or settype()
to convert the type.
When the variable type is cast by adding the target type enclosed in parentheses before the variable, the variable types allowed in the parentheses are as follows:
(int)
, (integer)
: Convert to integer type;
(bool)
, (boolean)
: Convert to Boolean type;
(float)
, (double)
, (real)
: Convert to floating point type;
(string)
: Convert to string type;
(array)
: Convert to array type;
(object)
: Convert to object type.
Use specific conversion functionsintval()
、floatval()
、boolval()
、 strval()
When converting the type of a variable, the functions of these functions are as follows:
intval(): used to obtain 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 a variable.
[Example] Use the above functions to force the type of variables.
<?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
The two forced type conversion methods described above will not change the type of the converted variable itself, but will convert the new type of data obtained by the conversion. When assigned to a new variable, the type and value of the original variable remain unchanged.
If you need to change the type of the variable itself, you can use the settype()
function. The syntax format of the function is as follows:
settype(mixed &$var, string $type)
Among them, $var is the variable to be converted. ; $type is the type to be converted, which can be boolean (bool)
, integer (int)
, float (double)
, string
, array
, object
, null
.
[Example] Use the settype() function to force the type of a variable.
<?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
There are the following points to note when using forced type conversion:
Integer type is converted to floating point type. The precision range of floating point type is much larger than that of integer type, so the precision after conversion will not change;
When floating point type is converted to integer type, the decimal part will be automatically discarded and only the integer will be retained. part. If a floating point number exceeds the valid range of integer numbers, the result will be undefined;
When a string is converted to a number, it is from the beginning of the string to the first one that is not a number character ends (the non-matching content is cleared), that is to say, if the first character of the string is not a number, the converted result is 0;
#NULL value is converted to a string , the result is a null character.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to force data type conversion in php. For more information, please follow other related articles on the PHP Chinese website!