Section 2. Numerical types of data types

  • 2020-05-16 06:31:50
  • OfStack

PHP supports eight basic data types.

Four scalar types:

boolean (Boolean) integer (integer) float (floating point, also known as double) string (string)

Two composite types:

array (array) object (object)

Finally, there are two special types:

resource (resources) NULL (NULL)

boolean data type:

The value can only be True or False. When other types are converted to boolean, the following values are considered as FALSE :

the Boolean value FALSE itself the integer value 0 (zero) the floating point value 0.0 (zero) Empty string, and the string "0" An array that does not contain any elements Objects that do not include any member variables (PHP 4.0 only) Special type NULL (including variables that have not yet been set) SimpleXML objects generated from XML documents without any markup (tags)

All other values are considered to be TRUE (including any resource).

integer data type:

Integer values can be represented in hexadecimal, hexadecimal, or hexadecimal, preceded by an optional symbol (- or +).

Base 8 means you must add 0 (zero) before the number, and base 106 means you must add 0x before the number.

The word length of an integer depends on the platform, although the maximum is usually about 21 billion (32-bit signed). PHP does not support unsigned integers. The word length of Integer value can be represented by the constant PHP_INT_SIZE. Since PHP 4.4.0 and PHP 5.0.5, the maximum value can be represented by the constant PHP_INT_MAX.

If a given number of 1 exceeds the range of integer, it will be interpreted as float. It also returns float if the result of the operation performed is outside the scope of integer.

There is no divisible operator in PHP. 1/2 produces float 0.5. You can always discard the decimal part, or use the round() function.

To explicitly convert a value to integer, cast it using (int) or (integer). In most cases, however, casts are not required because the values are automatically converted when an operator, function, or process control requires an integer parameter. You can also use the function intval() to convert a value to an integer.

From Boolean conversion, FALSE will produce 0 (zero), and TRUE will produce 1 (one). Convert from a floating point number, when converted from a floating point number to an integer, round to zero. If the floating point number goes beyond the integer range (usually +/ -2.15 e+9 = 2^31), the result is inconclusive because there is not enough precision for the floating point number to give an exact integer result. In this case there is no warning or even any notification!

float data type

The word length of floating point Numbers is platform-dependent, although the maximum is usually 1.8e308 and has the precision of a 14-bit hexadecimal number (64-bit IEEE format).

Obviously a simple base 10 score like 0.1 or 0.7 cannot be converted to an internal base 2 format without losing a bit of precision of 1. This can lead to confusing results: for example, floor((0.1+0.7)*10) usually returns 7 instead of the expected 8, because the internal representation of the result is actually something like 7.9.

This has to do with the fact that it is impossible to accurately express some base 10 fractions in finite digits. For example, 1/3 of the base 10 becomes 0.3.

So never believe that floating point Numbers are accurate to the last bit, and never compare two floating point Numbers for equality. If you really need higher precision, you should use the arbitrary precision mathematical function or the gmp function.


Related articles: