Php variable type program code details. In php, it is easy to check the variable type. you can use the gettype () function to return the current variable type, next I will introduce to you in detail how to use the gettype function to check the variable type in php. it is very easy to use the gettype () function to return the current variable type, next I will introduce to you in detail how to use the gettype function to check the variable type. For more information, see.
String gettype (mixed $ var) returns the PHP variable type var.
Example
The code is as follows: |
|
Function get_type ($ var) { If (is_object ($ var )) Return get_class ($ var ); If (is_null ($ var )) Return 'null '; If (is_string ($ var )) Return 'string '; If (is_array ($ var )) Return 'array '; If (is_int ($ var )) Return 'integer '; If (is_bool ($ var )) Return 'boolean '; If (is_float ($ var )) Return 'float '; If (is_resource ($ var )) Return 'resource '; // Throw new NotImplementedException (); Return 'unknown '; } ?> |
The official saying: do not use gettype () to test a type, because the returned string may need to be changed in future versions. In addition
Compared with the string, it runs slowly.
Use the is _ * function instead.
The code is as follows: |
|
/** * Returns the type of the var passed. * * @ Param mixed $ var Variable * @ Return string Type of variable */ Function myGetType ($ var) { If (is_array ($ var) return "array "; If (is_bool ($ var) return "boolean "; If (is_float ($ var) return "float "; If (is_int ($ var) return "integer "; If (is_null ($ var) return "NULL "; If (is_numeric ($ var) return "numeric "; If (is_object ($ var) return "object "; If (is_resource ($ var) return "resource "; If (is_string ($ var) return "string "; Return "unknown type "; } ?> |
Other variable types
Array_key_exists (mixed key, array search): // check whether the given key name or index exists in the array to determine the data type
Is_numeric (mixed var): // checks whether the measurement variable is a digital or digital string.
Is_bool ($ var): // checks whether the measurement variable is Boolean.
Is_float ($ var): // checks whether the variables are float and is_double, and is_real ().
Is_int ($ var): // checks whether the measurement variable is in the same usage as is_integer ().
Is_string ($ var): // checks whether the variable is a string.
Is_object ($ var): // checks whether the measurement variable is an object.
Is_array ($ var): // checks whether the measurement variable is an array.
Is_null ($ var): // checks whether the measurement variable is null.
The evaluate () function returns the current variable type. next I will introduce you to the gettype function to check the variable...