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(Integer)
- Float(Float type, also knownDouble)
- String(String)
Two composite types:
- Array(Array)
- Object(Object)
There are two special types:
- Resource(Resource)
- NULL(NULL)
BOoleanData type:
The value can only be True or False. when other types are converted to the boolean type, the following values are consideredFALSE
:
- TheBooleanValue
FALSE
Itself
- TheInteger0 (0)
- TheFloating point typeValue: 0.0 (0)
- NullString, AndString"0"
- Excluding any elementArray
- Excluding any member variablesObject(Applicable only to PHP 4.0)
- Special typeNULL(Including unconfigured variables)
- Generated from an XML document without any tagsSimpleXMLObject
All other values are consideredTRUE
(Including anyResources).
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.IntegerThe character length of a 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 exceedsIntegerWill be interpretedFloat. Similarly, if the execution result exceedsIntegerRange.Float.
PHP does not have the division operator.1/2GenerateFloat 0.5. You can always discard the decimal part, or useRound ()Function.
To explicitly convert a valueInteger, Use(Int)Or(Integer)Forced conversion. However, in most cases, no forced conversion is required, because when an operator, function, or flow control requiresIntegerThe value is automatically converted. You can also use functionsIntval ()To convert a value to an integer.
SlaveBoolean valueConversion,
FALSE
Will generate
0(Zero ),
TRUE
Will generate
1(1 ). SlaveFloating point numberTo convert 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.