PHP data type conversion and type detection

Source: Internet
Author: User
Tags scalar
This article mainly to share with you PHP data type conversion and type detection, although the basic knowledge of PHP, but very useful, I hope to help everyone.

Implicit conversions (auto-conversion)

Convert to String type

True--' 1 '

False, null, and empty string

Convert to numeric type

Start with a valid value and take it to the end of the first illegal value;

Start with a non-legal value and convert to 0

Convert to Boolean type

0, 0.0, ', ', ' 0 ', NULL, array () is false;

String, Integer, floating-point number, ' 0.0 ', ' false ', ' true '.

Show transformations

Temporary conversion

1. (variable type) $ variable name

Grammar:

Integer: (int) $ variable name

Float type: (float) $ variable name

Character type: (string) $ variable name

Boolean type: (BOOL) $ variable Name

Null: (unset) $ variable name

Number: (array) $ variable name

Object: (object) $ variable name

2. System function implementation

Intval ($ variable name) returns the value of the variable converted to an integer

Floatval ($ variable name) returns the value of a variable converted to a floating-point type

Strval ($ variable name) returns the value of the variable converted to a string type

Oolval ($ variable name) returns the value of the variable converted to a Boolean type

Permanent conversion

Settype ($ variable name, ' type ') sets the type of the variable

Detecting variable types

Integer type Is_int ()

Floating-point is_float ()

String is_string ()

Boolean type Is_bool ()

Scalar type is_scalar ()

Empty Is_null ()

Array Is_array ()

Object Is_object ()

Resource Is_resource ()

Numeric Is_numeric () in numeric or string form

Implicit conversions (auto-conversion)

Convert to String type

True--' 1 '

False, null, and empty string

Convert to numeric type

Start with a valid value and take it to the end of the first illegal value;

Start with a non-legal value and convert to 0

Convert to Boolean type

0, 0.0, ', ', ' 0 ', NULL, array () is false;

String, Integer, floating-point number, ' 0.0 ', ' false ', ' true '.

Show transformations

Temporary conversion

1. (variable type) $ variable name

Grammar:

Integer: (int) $ variable name

Float type: (float) $ variable name

Character type: (string) $ variable name

Boolean type: (BOOL) $ variable Name

Null: (unset) $ variable name

Number: (array) $ variable name

Object: (object) $ variable name

2. System function implementation

Intval ($ variable name) returns the value of the variable converted to an integer

Floatval ($ variable name) returns the value of a variable converted to a floating-point type

Strval ($ variable name) returns the value of the variable converted to a string type

Oolval ($ variable name) returns the value of the variable converted to a Boolean type

Permanent conversion

Settype ($ variable name, ' type ') sets the type of the variable

Detecting variable types

Integer type Is_int ()

Floating-point is_float ()

String is_string ()

Boolean type Is_bool ()

Scalar type is_scalar ()

Empty Is_null ()

Array Is_array ()

Object Is_object ()

Resource Is_resource ()

Numeric Is_numeric () in numeric or string form

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.