Conversion and detection of PHP data types

Source: Internet
Author: User

This article mainly introduces the PHP data type conversion and detection, has a certain reference value, now share to everyone, the need for friends can refer to

1. Conversion of data types

Although PHP is a weakly typed language, it is also possible to convert data in the same way as the C language, simply by enclosing the variable with the type name in parentheses.

    1. Convert to Boolean type: (Boolean)

      Str

    2. Convert to Character type: (String)

      Flo

    3. Convert to Integer type: (integer)

      Str

    4. Convert to Floating point type: (float) $str

    5. Convert an array: such as (arrays) $str

    6. Convert to object: (Obiect) $str

Note: In the process of type conversion, you should be aware of the following: when converted to Boolean, NULL, 0, and unassigned variables or arrays are converted to false, and the others are true; When converting to an integer, the Boolean conversion to 0,true is converted to 1, and the decimal portion of the floating-point type is removed, and the character type is truncated to a non-number-bit if it starts with a number, otherwise the output is 0.

Another type conversion can also be set by the Settype function.
BOOL Settype (mixed var,string type)
Note: The parameter var is the specified variable; The parameter type is the specified data type. The parameter type has 7 optional values, Boolean, float,. Integer, array, null, object, and string. If the conversion succeeds, the Settype () function returns True, otherwise false is returned.
When a string is converted to an integer or float, if the string starts with a number, it first converts the number part to an integer, and then goes back to the string; If the number contains a decimal point, it is taken to the previous digit in the decimal point.

2, the data type detection

    1. is bool checks if the variable is a Boolean type: is bool (true), Is_bool (false)

    2. is_string Check if the variable is a string type: is_string (' string ') is_string (1234)

    3. is float/is double checks if the variable is a floating-point type: Is_float (3.1415), Is_float (3 1415)

    4. Is_integer/is_int Check if the variable is an integer: such as Is_integer (34), Is_integer (' 34 ')

    5. Is_null checks if the variable is null: Is_null (NULL)

    6. Is_array Check if the variable is of the array type: I_sarray ($arr)

    7. Is_object Check if the variable is an object type: Is_object ($obj)

    8. Is_numeric checks whether a variable is a number or a string consisting of a number: such as Is_numeric (' 5 '), Is_numeric (' Aabb ')

Related Article

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.