Two major functions of programming languages are data processing and process control. data types and interfaces are the basis for data processing. process control is a variety of control statements. program organization and coordination is a variety of programming paradigms; this section describes the basic data types of PHP.
PHP supports eight basic data types.
Four scalar types:
- Boolean (boolean)
- Integer)
- Float (float, also called double)
- String (string)
Two composite types:
There are two special types:
BOoleanData type:
The value can only be True or False. when other types are converted to the boolean type, the following values are consideredFALSE
:
- The Boolean value
FALSE
Itself
- The integer value 0 (0)
- The floating point value is 0.0 (0)
- Empty string and string "0"
- Array that does not contain any elements
- Objects that do not include any member variables (applicable only to PHP 4.0)
- Special type NULL (including unset variables)
- SimpleXML object generated from an XML document without any tags
All other values are consideredTRUE
(Including any resources ).
Integer data type:
The integer value can be expressed in decimal, hexadecimal, or octal format. you can add an optional symbol (-or +) to the front ).
The octal value indicates that the value must be added before the number.0(0), which must be added before a number in hexadecimal notation0x.
The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-bit signed ). PHP does not support unsigned integers. The length of an Integer value can be a constant.PHP_INT_SIZE
The maximum value can be a constant after PHP 4.4.0 and PHP 5.0.5.PHP_INT_MAX
.
If a given number exceeds the integer range, it is interpreted as float. Similarly, if the execution result exceeds the integer range, float is returned.
PHP does not have the division operator.1/2Generate float0.5. You can always discard the fractional part or use the round () function.
To explicitly convert a value to an integer, use(Int)Or(Integer)Forced conversion. However, in most cases, no forced conversion is required, because the value is automatically converted when an integer parameter is required for the operator, function, or flow control. You can also convert a value to an integer using the intval () function.
Convert from a Boolean value,
FALSE
Will generate
0(Zero ),
TRUE
Will generate
1(1 ). Converts a floating point to an integer.
Toward ZeroInteger. If the floating point number exceeds the integer range (usually
+/-2.15e + 9 = 2 ^ 31), The result is uncertain, because there is not enough precision to make the floating point number give an exact integer result. In this case, there is no warning or even no notification!
Float data type
The length of the floating point is related to the platform, although the maximum value is usually 1.8e308 with 14-bit decimal digits (64-bit IEEE format ).
Obviously, the simple decimal score is like0.1Or0.7It cannot be converted to an internal binary format without losing a little bit of precision. This results in confusion: for example,Floor (0.1 + 0.7) * 10)Usually7Not as expected8Because the internal representation of the result is similar.7.9.
This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example1/3Changed0.3.
Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. If higher precision is required, use any precision mathematical function or gmp function.