Home >Backend Development >PHP Tutorial >PHP data type conversion study notes_PHP tutorial

PHP data type conversion study notes_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:50:03814browse

This article will introduce you to the PHP data type conversion study notes. If you don’t understand PHP data types, you can refer to them.

Commonly used data types include string type, floating point type (single-precision floating-point type float, double-precision floating-point type double), integer, and Boolean type

1 Convert other types to integers
1.1 Convert string type to integer type
1.1.1 If all the strings are numbers, they will be directly converted to integers. If there is a decimal point, the content after the decimal point will be removed. For example, '12' and '12.3' will both be 12
after being converted to integers. 1.1.2 If the string starts with a letter, no matter how many digits or decimal points there are in the middle or behind, it will be 0 after conversion to an integer. For example, 'ab12.3c' is converted to integer and is 0
1.1.3 If it starts with a number, the characters after the number will be removed. If there is a decimal point, the characters after the decimal point will be removed. For example, '123.4abc' and '123abc' are both 123

after conversion to integer

1.2 Convert floating point type to integer type

1.2.1 If the floating point number contains a decimal point, the characters after the decimal point will be removed. If not, it will be directly converted into an integer. For example: the floating point number 12.6 is converted into an integer and is 12


1.3 Convert Boolean type to integer type
1.3.1 When the Boolean type is converted to an integer type, true will be converted to 1 and false will be converted to 0. NULL is converted to an integer type and is 0. So if you want to judge if (NULL == 0) {echo "NULL equals 0";} It will return NULL equal to 0


2 Convert other types to string type
2.1 To convert integer or floating point type to string type, add quotation marks directly to the number. For example, 12 12.3 is converted to string type and becomes "12" "12.3"
2.2 When converting a Boolean type to a string, true will be converted into the string "1", false and NULL will be converted into a string, and the result will be an empty string, and there will be no output

3 Convert other types to floating point types
3.1 The integer type is directly converted to a floating point type, and the numerical value remains unchanged. The conversion of a string to a floating point type is basically the same as the conversion of a string to an integer type, except that when there is a decimal point between the strings, the decimal point will be saved. For example, "12.3abc" is converted to 12.3. The method for other forms is the same
3.2 When converting Boolean type to floating point type, true will be converted to floating point type 1, false and NULL will be converted to floating point type and the result will be 0

4 Convert other types to Boolean
4.1 Empty strings are converted to Boolean type as FALSE, non-empty type is TRUE; integer and floating point 0 are converted into Boolean type as FALSE, others are TRUE

4.2 The result after NULL is converted to Boolean is FALSE

PHP data type conversion is a forced conversion. The PHP data types that are allowed to be converted are:

•(int), (integer): Convert to integer
•(float), (double), (real): Convert to floating point type
•(string): Convert to string
•(bool), (boolean): Convert to Boolean type
•(array): Convert to array
•(object): Convert to object
PHP data types have three conversion methods:

•Place the target type in parentheses before the variable to be converted
•Use 3 specific type conversion functions, intval(), floatval(), strval()
•Use the general type conversion function settype(mixed var,string type)
The first conversion method: (int) (bool) (float) (string) (array) (object)

The code is as follows Copy code
1.
代码如下 复制代码
1. 2.$num1=3.14;
3.$num2=(int)$num1;
4.var_dump($num1); //输出float(3.14)
5.var_dump($num2); //输出int(3)
6.?>
2.$num1=3.14;

3.$num2=(int)$num1;

4.var_dump($num1); //Output float(3.14)
 代码如下 复制代码
1. 2.$str=”123.9abc”;
3.$int=intval($str); //转换后数值:123
4.$float=floatval($str); //转换后数值:123.9
5.$str=strval($float); //转换后字符串:”123.9″
6.?>
5.var_dump($num2); //Output int(3) 6.?> The second conversion method: intval() floatval() strval()
The code is as follows Copy code
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.?>

The third conversion method: settype();

The code is as follows
 代码如下 复制代码

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

Copy code
1. 2.$num4=12.8;
3.$flg=settype($num4,”int”);
4.var_dump($flg); //Output bool(true)
5.var_dump($num4); //Output int(12)
6.?>

http://www.bkjia.com/PHPjc/632668.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632668.html
TechArticle
This article will introduce you to the PHP data type conversion study notes. If you don’t understand the PHP data types, please Enter for reference. Commonly used data types include string type and floating point type (single...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn