PHP Data Type Conversion Learning Note _php Tutorial

Source: Internet
Author: User
This article to the students to introduce PHP data type conversion Learning notes, if you know the PHP data type is not clear to enter the reference OH.

Common data types are string, float (single-precision float float, double-precision floating-point double), shaped, Boolean

1 other types converted to integral type
1.1 String conversion to Integer type
1.1.1 If the string is all numeric, it is converted directly to integer, and if there is a decimal point, the content after the decimal point is removed, such as ' 12 ' and ' 12.3 ' are 12 after conversion to integer type.
1.1.2 If the string starts with a letter, no matter how many digits or decimal points are in the middle or behind, the conversion is 0 after shaping. 0 if ' ab12.3c ' is converted to an integral type
1.1.3 If you start with a number, the character after the number is removed, and if there is a decimal point, it is removed after the decimal point. such as ' 123.4abc ', ' 123abc ' are converted to 123 after shaping

1.2 Floating-point conversion to integral type

1.2.1 If the floating-point number contains a decimal point, the character after the decimal point is removed, and if not, the integer is changed directly. For example: floating point 12.6 converted to 12 after shaping


1.3 Boolean conversion to integral type
When a 1.3.1 Boolean is converted to an integer, true is converted to 1 false and converted to 0, where NULL is converted to int after 0, so if you want to determine if (null = = 0) {echo "null equals 0"} is returned null equals 0


2 other types converted to String type
2.1 Integer, floating-point conversion to string type is directly on the number of quotes such as 12 12.3 converted to a string after the "12" "12.3"
2.2 Boolean converted to string yes, true converts to string "1", false with null converted to string result to null string, no output

3 other types converted to floating-point type
3.1 The integer is converted directly to floating point type, the value is constant, the string conversion to float type is basically the same as string conversion to Integer, except that the decimal point is saved when there is a decimal point between the strings. such as "12.3ABC" after conversion for 12.3 Other forms is the same method
3.2 Boolean converts to floating-point type Yes, true translates to float type 1, false with null conversion to floating-point result of 0

4 other types converted to Boolean
4.1 An empty string is converted to a Boolean of false, non-null is true, and integer-to-float 0 is converted to a Boolean of false, and the other is true

4.2 Null converted to Boolean results to False

PHP's data type conversions are cast, and the PHP data types that are allowed to be converted are:

• (int), (integer): Convert to Reshape
• (float), (double), (real): Convert to floating-point
• (string): Convert to String
• (bool), (Boolean): Convert to Boolean type
• (array): Convert an array
• (object): Convert to Object
There are three ways to convert PHP data types:

• Precede the variable you want to convert with a target type enclosed in parentheses
• Use 3 specific types of conversion functions, intval (), Floatval (), Strval ()
• Use the common type conversion function Settype (mixed var,string type)
First conversion Mode: (int) (bool) (float) (string) (array) (object)

The code is as follows Copy Code
1. 2. $num 1=3.14;
3. $num 2= (int) $num 1;
4.var_dump ($num 1); Output float (3.14)
5.var_dump ($num 2); Output int (3)
6.?>

Second mode of conversion: Intval () floatval () Strval ()

The code is as follows Copy Code
1. 2. $str = "123.9ABC";
3. $int =intval ($STR); Post-conversion value: 123
4. $float =floatval ($STR); Post-conversion value: 123.9
5. $str =strval ($float); Converted string: "123.9″
6.?>

The third mode of conversion: Settype ();

The code is as follows Copy Code

1. 2. $num 4=12.8;
3. $flg =settype ($num 4, "int");
4.var_dump ($FLG); output bool (TRUE)
5.var_dump ($num 4); Output int (12)
6.?>

http://www.bkjia.com/PHPjc/632668.html www.bkjia.com true http://www.bkjia.com/PHPjc/632668.html techarticle This article to the students to introduce PHP data type conversion Learning notes, if you know the PHP data type is not clear to enter the reference OH. Commonly used data types are string, floating point type (single ...

  • 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.