Php Data type conversion course NOTE _ PHP Tutorial

Source: Internet
Author: User
Php Data type conversion learning notes. This article will introduce you to the learning notes on php Data type conversion. if you do not know the php Data type clearly, please refer. Common data types include string and floating point data types (this document will introduce you to learning notes on php Data type conversion. if you do not know the php Data type, go to the reference page.

Common data types include string, float, float, integer, and Boolean.

1. convert other types to integer types
1.1 convert string type to integer type
1.1.1 if all strings are numbers, they are directly converted to integers. if there is a decimal point, the decimal point is removed, such as '12' and '12. 3' after being converted to an integer, all values are 12
1.1.2 if a string starts with a letter, no matter how many digits or decimal points are in the middle or back, the value is 0 after being converted to an integer. For example, 'ab12. 3c 'is converted to an integer and then 0.
1.1.3 if it starts with a number, the characters after the number are removed. if there is a decimal point, the characters after the decimal point are removed. For example, after converting '192. 4abc' and '123abc' to an integer, all values are 123.

1.2 float type to integer type

1.2.1 if a floating point contains a decimal point, remove the character after the decimal point. if not, convert it to an integer. For example, if the floating point number 12.6 is converted to the integer value 12


1.3 convert Boolean type to integer type
1.3.1 if the Boolean type is converted to an integer type, true is converted to 1. false is converted to 0. NULL is converted to an integer type and then 0. Therefore, if you want to determine if (NULL = 0) {echo "NULL equals 0";} returns NULL equals 0


2. convert other types to string types
2.1 integer and floating point type conversion to string type is to directly add quotation marks on the number, for example, 12 12.3 after conversion to string is "12" "12.3"
2.2 if the Boolean type is converted to a string, true is converted to the string "1", false and NULL are converted to an empty string and no output is available.

3. convert other types to floating point types
3.1 Integers are directly converted to float type, with the numeric value unchanged. string conversion to float type is basically the same as string conversion to integer type, except that when there is a decimal point between strings, the decimal point will be saved. For example, if "12.3abc" is converted to "12.3", other forms are the same.
3.2 if the Boolean type is converted to the floating point type, true is converted to the floating point type 1, and false and NULL are converted to the floating point type 0.

4 convert other types to Boolean
4.1 convert an empty string to a Boolean value of FALSE or a non-empty value of TRUE. convert the value 0 of the integer and floating point types to a Boolean value of FALSE, and set the value other values to TRUE.

4.2 If NULL is converted to a Boolean value, the result is FALSE.

PHP Data type conversion is a forced conversion. The PHP Data types that can be converted include:

• (Int), (integer): convert to integer
• (Float), (double), (real): Converts to float
• (String): convert to a string
• (Bool), (boolean): convert to boolean type
• (Array): convert to an array
• (Object): convert to an object
There are three conversion methods for PHP Data types:

• The Target type enclosed in parentheses before the variable to be converted
• Three types of conversion functions are used: intval (), floatval (), and strval ()
• Use the common type conversion function settype (mixed var, string type)
First conversion method: (int) (bool) (float) (string) (array) (object)

The code is as follows:
1. 1. $ num1 = 3.14;
3. $ num2 = (int) $ num1;
4. var_dump ($ num1); // output float (3.14)
5. var_dump ($ num2); // output int (3)
6.?>

Second conversion method: intval () floatval () strval ()

The code is as follows:
1. 2. $ str = "123.9abc ";
3. $ int = intval ($ str); // converted value: 123
4. $ float = floatval ($ str); // converted value: 123.9
5. $ str = strval ($ float); // converted string: "123.9 ″
6.?>

Third conversion method: settype ();

The code is as follows:

1. 1. $ num4 = 12.8;
3. $ flg = settype ($ num4, "int ");
4. var_dump ($ flg); // output bool (true)
5. var_dump ($ num4); // output int (12)
6.?>

Bytes. Common data types include string and floating point (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.