PHP supports 8 basic data types.
Four scalar types:
Two composite types:
Finally there are two special types:
booleanData type:
The value can only be True or False. When other types are converted to boolean types, the following values are considered FALSE
:
FALSE
itself
All other values are considered TRUE
(including any resource).
integer data type:
Integer values can be represented in decimal, hexadecimal or octal notation, preceded by an optional symbol (- or ).
Octal means that the number must be preceded by 0 (zero), and hexadecimal means that the number must be preceded by 0x.
The word size of integers is platform-dependent, although the usual maximum is about two billion (32-bit signed). PHP does not support unsigned integers. Integer The word length of the value can be represented by the constant PHP_INT_SIZE
, since PHP 4.4.0 and PHP 5.0 After .5, the maximum value can be represented by the constant PHP_INT_MAX
.
If a given number exceeds the range of integer, it will be interpreted as float. Similarly, if the result of the operation exceeds the range of integer, float.
There is no integer division operator in PHP. 1/2 produces float 0.5. You can always discard the fractional part, or use the round() function.
To explicitly convert a value to integer , use (int) or (integer) to cast. In most cases, however, casting is not necessary, because when an operator, function, or flow control requires a integer parameter, the value will be converted automatically. You can also use the function intval() to convert a value to an integer type.
FALSE
will produce 0 (zero), TRUE
Will produce 1 (one). float data type
The word size of floating-point numbers is platform-dependent, although typically the maximum value is 1.8e308 with a precision of 14 decimal digits (64-bit IEEE format).
Apparently simple decimal fractions like 0.1 or 0.7 cannot be converted to the internal binary format without losing a little precision. This can lead to confusing results: for example, floor((0.1 0.7)*10) will usually return 7 instead of the expected 8 , because the internal representation of the result is actually similar to 7.9.
This is related to the fact that it is impossible to express certain decimal fractions accurately with a finite number of digits. For example, 1/3 in decimal becomes 0.3.
So never trust that a floating-point number result is accurate to the last digit, and never compare two floating-point numbers to see if they are equal. If you really need higher precision, you should use arbitrary precision math functions Or gmp function.