Home > Article > Backend Development > PHP determines variable type
Recommended Manual: php complete self-study manual
##php Commonly used functions for judging variables include gettype(), is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar() and is_string() .
The gettype() function returns the type of the variable, such as "boolean", "integer", "double" (the float type will return "double" instead of "float"), "string", "array" ", "object", "resource", "NULL" and "unknown type" and other values indicate the variable type, such as:$var = "coding"; echo gettype($var);//stringwhile other functions confirm a certain variable type and return true or false, for example:
$var1=321; var_dump(is_numeric($var1));//bool(true) $var2="123"; var_dump(is_numeric($var2));//bool(true)Note: is_numeric() is used to determine whether the variable is a number or a numeric stringAttachment: type conversionIf you say gettype( ) is used to obtain the variable type, then settype() is obviously to set the variable type. Here are several type conversion methods:
1.settype($var,type) function conversion
$var='124das.21321'; settype($var,'int');//第二个参数指定转换类型,可以是int、float、bool、string、array和object var_dump($var);//int 124
2. Target type specified conversion
(int)$var;//括号内指定转换类型,可以是int、float、bool、string、array和object
3. Three specific type function conversion
intval($var) , floatval($var) and strval($var)$var = '121.212sdsa'; var_dump(floatval($var)); // float 121.212
Recommended related articles: 1.
How does PHP use the gettype() function to determine the type of a variable? 2.
How does php determine the file type 3.
php implementation to determine client type
Related video recommendations: 1.
Dugu Jiujian (4)_PHP video tutorial
The above is the detailed content of PHP determines variable type. For more information, please follow other related articles on the PHP Chinese website!