Home > Article > Backend Development > PHP data type conversion and type detection
This article mainly shares with you PHP data type conversion and type detection. Although it is the basic knowledge of PHP, it is very useful and I hope it can help everyone.
Implicit conversion (automatic conversion)
Convert to string type
true --> '1'
false, null --> ; Empty string
is converted into a numeric type
starts with a legal value and ends with the first illegal value;
starts with an illegal value and is converted to 0
Convert to Boolean type
0, 0.0, '', "", '0', null, array() is false;
String, integer, float Points, '0.0', 'false', ' ' are true.
Display conversion
Temporary conversion
1. (Variable type)$Variable name
Syntax:
Integer: (int) $Variable name
Floating point type: (float)$Variable name
Character type: (string)$Variable name
Boolean type: (bool)$Variable name
Empty: (unset)$Variable name
Number: (array)$Variable name
Object: (object)$Variable name
2. System Function implementation
intval($variable name) Returns the value of the variable converted to an integer
floatval($variable name) Returns the value of the variable converted to a floating point type
strval( $Variable name) Returns the value of the variable converted to string type
oolval($Variable name) Returns the value of the variable converted to Boolean type
Permanent conversion
settype($ Variable name, 'type') Set the type of the variable
Detect the variable type
Integer type is_int()
Floating point type is_float()
Character String is_string()
Boolean type is_bool()
Scalar type is_scalar()
Empty is_null()
Array is_array()
Object is_object()
Resource is_resource()
Numeric or string value is_numeric()
Implicit conversion (automatic conversion)
Convert to string type
true --> '1'
false, null --> Empty string
Convert to numeric type
Start with a legal value and continue until the first illegal value;
Start with an illegal value and convert it to 0
Convert to Boolean type
0 , 0.0, '', "", '0', null, array() are false;
String, integer, floating point number, '0.0', 'false', ' ' are true.
Display conversion
Temporary conversion
1. (Variable type)$Variable name
Syntax:
Integer: (int) $Variable name
Floating point type: (float)$Variable name
Character type: (string)$Variable name
Boolean type: (bool)$Variable name
Empty: (unset)$Variable name
Number: (array)$Variable name
Object: (object)$Variable name
2. System Function implementation
intval($variable name) Returns the value of the variable converted to an integer
floatval($variable name) Returns the value of the variable converted to a floating point type
strval( $Variable name) Returns the value of the variable converted to string type
oolval($Variable name) Returns the value of the variable converted to Boolean type
Permanent conversion
settype($ Variable name, 'type') Set the type of the variable
Detect the variable type
Integer type is_int()
Floating point type is_float()
Character String is_string()
Boolean type is_bool()
Scalar type is_scalar()
Empty is_null()
Array is_array()
Object is_object()
Resource is_resource()
Numeric or string value is_numeric()
Related recommendations:
JavaScript data type conversion principles detailed explanation
PHP data type conversion conversion
JS data type conversion summary
The above is the detailed content of PHP data type conversion and type detection. For more information, please follow other related articles on the PHP Chinese website!