PHP Detailed Explanation of Program Code to Determine Variable Type_PHP Tutorial

WBOY
Release: 2016-07-13 17:15:13
Original
719 people have browsed it

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
 代码如下 复制代码

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';
}
?>

Copy code

function get_type($var)

{

If(is_object($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";
}
?>

           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';

}

?>

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

www.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 of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!