PHP Data Type Conversion Learning Notes

Source: Internet
Author: User

Commonly used data types are string type, floating-point type (single-precision floating-point float, double-precision floating-point double), shaping, Boolean

1 other types convert to integral type
1.1 String type converted to integral type
1.1.1 If the string is all numeric, it is converted directly to an integral type, and if there is a decimal point, the content after the decimal point is removed, such as ' 12 ' and ' 12.3 ' converted to integral type 12
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 the shape is shaped. If ' ab12.3c ' is converted to an integral type, 0
1.1.3 If you start with a number, the character behind the number is removed, and the decimal point is removed after the decimal point. such as ' 123.4abc ', ' 123abc ' converted to plastic after 123

1.2 Floating-point type converted to integral type

1.2.1 If the floating-point number contains a decimal point, remove the character after the decimal point, and if not, change to an integral type directly. such as: floating-point number 12.6 converted to plastic after the 12


1.3 Boolean converted to integral type
When the 1.3.1 Boolean is converted to an integral type, true converts to 1 false to 0 where Null is converted to an integral type and 0 so if you want to determine if (Null = 0) {echo null equals 0;} that returns null equals 0.


2 other types convert to string type
2.1 Integer, floating-point conversion to string is directly in the number of quotes such as 12 12.3 converted to a string followed by "12" "12.3"
2.2 Boolean converted to string Yes, true is converted to string "1", False to Null to string result to null string, no output

3 other types are converted to floating-point type
The 3.1 integral type converts directly to a floating-point type, the value is unchanged, and the string is converted to a floating-point type, which is basically the same as a string conversion to an integral type, except that the decimal point is saved when there is a decimal point between the strings. 12.3 Other forms are the same way after "12.3ABC" conversion
3.2 Boolean to floating-point type Yes, true to floating-point type 1, false to floating-point with null result 0

4 other types convert to Boolean
4.1 Empty string to Boolean to False, Non-null to True, integer to 0 of floating-point type to false, others to true

4.2 Null converted to Boolean result is False

PHP data type conversions are cast, and PHP data types that allow conversion are:

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

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

The code is as follows Copy Code
1.<?php
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.?>

The second mode of conversion: Intval () floatval () Strval ()

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

The third mode of conversion: Settype ();

  code is as follows copy code

1.<?php
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 ()   
6.?

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.