Home > Article > Backend Development > PHP Detailed Explanation of Program Code to Determine Variable Type_PHP Tutorial
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.
Example
The code is as follows
|
Copy code
|
||||||||
function get_type($var) { If(is_object($var))
If(is_null($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'; }?>
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.
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 Previous article:PHP prompt Notice: Undefined index error solution_PHP tutorialNext article:PHP prompt Notice: Undefined index error solution_PHP tutorial Related articlesSee more |