This article mainly introduces the PHP data type conversion and detection, has a certain reference value, now share to everyone, the need for friends can refer to
1. Conversion of data types
Although PHP is a weakly typed language, it is also possible to convert data in the same way as the C language, simply by enclosing the variable with the type name in parentheses.
Convert to Boolean type: (Boolean)
Str
Convert to Character type: (String)
Flo
Convert to Integer type: (integer)
Str
Convert to Floating point type: (float) $str
Convert an array: such as (arrays) $str
Convert to object: (Obiect) $str
Note: In the process of type conversion, you should be aware of the following: when converted to Boolean, NULL, 0, and unassigned variables or arrays are converted to false, and the others are true; When converting to an integer, the Boolean conversion to 0,true is converted to 1, and the decimal portion of the floating-point type is removed, and the character type is truncated to a non-number-bit if it starts with a number, otherwise the output is 0.
Another type conversion can also be set by the Settype function.
BOOL Settype (mixed var,string type)
Note: The parameter var is the specified variable; The parameter type is the specified data type. The parameter type has 7 optional values, Boolean, float,. Integer, array, null, object, and string. If the conversion succeeds, the Settype () function returns True, otherwise false is returned.
When a string is converted to an integer or float, if the string starts with a number, it first converts the number part to an integer, and then goes back to the string; If the number contains a decimal point, it is taken to the previous digit in the decimal point.
2, the data type detection
is bool checks if the variable is a Boolean type: is bool (true), Is_bool (false)
is_string Check if the variable is a string type: is_string (' string ') is_string (1234)
is float/is double checks if the variable is a floating-point type: Is_float (3.1415), Is_float (3 1415)
Is_integer/is_int Check if the variable is an integer: such as Is_integer (34), Is_integer (' 34 ')
Is_null checks if the variable is null: Is_null (NULL)
Is_array Check if the variable is of the array type: I_sarray ($arr)
Is_object Check if the variable is an object type: Is_object ($obj)
Is_numeric checks whether a variable is a number or a string consisting of a number: such as Is_numeric (' 5 '), Is_numeric (' Aabb ')