Php variable type program code explanation _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.