PHP Judgment Variable Type

Source: Internet
Author: User

PHP commonly used to determine the function of variables are GetType (), Is_array (), Is_bool (), Is_float (), Is_integer (), Is_null (), Is_numeric (), Is_object (), Is_ Resource (), Is_scalar (), and is_string ().
Where the GetType () function returns the type of the variable, such as "Boolean", "Integer", "Double" (The Float type returns "Double" rather than "float"), "string", "array", "Object", " Resource "," NULL ", and" unknown type "are equivalent, indicating the type of the variable, as an example:

$var = "coding"; Echo GetType ($var);//string

The other function is to confirm for a variable type, return True or false, as an example:

$var 1=321;var_dump (Is_numeric ($var 1)),//bool (true) $var 2= "123"; Var_dump (Is_numeric ($var 2));//bool (True)

Note: Is_numeric () is used to determine if a variable is a numeric or numeric string

Attached: type conversion

If GetType () is used to get the variable type, then Settype () is obviously setting the variable type, which summarizes several types of conversion methods:

1.settype ($var, type) function conversion

$var = ' 124das.21321 '; Settype ($var, ' int ');//The second parameter specifies the type of conversion, which can be int, float, bool, string, array, and Objectvar_dump ($var); int 124

2. Target type Specifies conversion

(int) $var;//Specify the type of conversion in parentheses, which can be int, float, bool, string, array, and object

3. Three specific types of function conversions

Intval ($var), Floatval ($var) and Strval ($var)

$var = ' 121.212SDSA '; Var_dump (Floatval ($var)); Float 121.212

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.