PHP Detailed Explanation of Program Code to Determine Variable Type_PHP Tutorial
WBOYOriginal
2016-07-13 17:15:13760browse
The method to check the variable type in PHP is very simple. Use the gettype() function to return the current variable type. Now I will introduce to you in detail how to use the gettype function to check the variable type. Friends who need to know more can refer to.
string gettype ( mixed $var ) returns the type of PHP variable var.
/**
* 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";
}
?>
Officially said: Do not use gettype() to test a certain type, because the string it returns may need to change in future versions. Additionally, due to the inclusion of
Without string comparison, its operation is also slower.
Use is_* functions instead.
The code is as follows
Copy code
/**<🎜>
* 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";<🎜>
}<🎜>
?>
Some other variable type judgment collections
array_key_exists(mixed key, array search) ://Check whether the given key name or index exists in the array Determine the data type
is_numeric (mixed var): //Check whether the measured variable is a number or a digital string
is_bool($ var): //Check whether the measured variable is of Boolean type
is_float($ var): //Check whether the measured variable is of floating point type. It has the same usage as is_double and is_real()
is_int($ var): //Check whether the measured variable is an integer. The same usage as is_integer()
is_string($ var): //Check whether the measured variable is a string
is_object($ var): //Check whether the measured variable is an object
is_array($ var): //Check whether the measured variable is an array
is_null($ var): //Check whether the measured variable is null
http://www.bkjia.com/PHPjc/628878.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628878.htmlTechArticleThe method to check the variable type in php is very simple. Use the gettype() function to return the current variable type. , let me introduce to you in detail how to use the gettype function to check variables...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn