PHP method for testing and setting variable types

Source: Internet
Author: User
Most variable functions are used to test the type of a function. PHP has two common functions: gettype () and settype (). These two functions have the following function prototype, through which you can obtain the parameters to be passed and the returned

Most variable functions are used to test the type of a function. PHP has two common functions: gettype () and settype (). These two functions have the following function prototype, through which you can obtain the parameters to be passed and the returned results:
String gettype (mixed var );
Bool settype (mixed var, string type );

To use the gettype () function, you must first pass a variable to it. It determines the type of the variable and returns a string containing the type name: bool, int, double (for float type), string, array, object, and resource. If the variable type is not one of the standard types, the function returns "unknow type (unknown type )".

To use the settype () function, you must first pass a variable of the type to be changed, and a string containing a type in the above type list. You can use these functions as follows:
$ A = 100;
Echo gettype ($ ).'
';
Settype ($ a, 'double ');
Echo gettype ($ ).'
';
?>

The code above indicates that $ a is an integer when the gettype () function is called for the first time. After the settype () function is called, it becomes a double-precision type. The running result is as follows.

PHP also provides some specific types of test functions. Each function uses a variable as its parameter and returns true or false. These functions are as follows:
1. is_array (): Check whether the variable is an array.
2. is_double (): is_float (): is_real () (all functions are the same): Check whether the variable is a floating point number.
3. is_long (): is_int (): is_integer () (all functions are the same): Check whether the variable is an integer.
4. is_string (): Check whether the variable is a string.
5. is_bool (): Check whether the variable is a Boolean value.
6. is_object (): Check whether the variable is an object.
7. is_resource (): Check whether the variable is a resource.
8. is_null (): Check whether the variable is Null.
9. is_scalar (): checks whether the variable is a scalar, that is, an integer, Boolean value, string, or floating point number.
10. is_numeric (): checks whether the variable is a numeric or numeric string of any type.
11. is_callable (): Check whether the variable is a valid function name.

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.