Home > Backend Development > PHP Tutorial > Data Types PHP Section 2 Numeric Data Types

Data Types PHP Section 2 Numeric Data Types

WBOY
Release: 2016-07-29 08:48:29
Original
1255 people have browsed it

PHP supports 8 basic data types.

4 Point type, also called

double
  • ) string
  • (string) Two composite types:
  • array (array) object
  • (object) Finally there are two special types:

    resource
  •  (resource) NULL
  •  (NULL) b
oolean

Data type:
  • The value can only be True or False. When other types are converted to boolean types, the following values ​​are considered FALSE
  • : the Boolean
  • value
FALSE

itself the whole type value 0 (zero)
the
float

value 0.0 (zero)

empty string, and

string
    "0"
  • an array containing no elements
  • An object that does not include any member variables (only applicable to PHP 4.0)
  • Special type NULL (including variables that have not been set)
  • generated from an XML document without any tags (tags) SimpleXML All other values ​​for an object are considered
  • TRUE (including any resources ).
  • integer data type:
  • Integer values ​​can be represented in decimal, hexadecimal or octal, and can be preceded by an optional sign (- or +). The octal representation must be preceded by 0 (zero), and the hexadecimal representation must be preceded by
  • 0x. The word length of integers is platform-dependent, although the usual maximum is about two billion (32-bit signed). PHP does not support unsigned integers. The word length of the
  • 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 exceeds the range of

integer

, it will be interpreted as

float

. Similarly, if the result of the operation exceeds the range of integer

,

float will also be returned. There is no operator for integer division in PHP. 1/2 yields

float

0.5. You can always discard the decimal part, or use the round() function. To explicitly convert a value to integer, use (int) or

(integer)

cast. Most of the time though, no casting is necessary, because when an operator, function, or control flow requires an integer parameter, the value is automatically converted. You can also use the function intval() to convert a value into an integer. Converting

from a

boolean , FALSE will yield 0 (zero), TRUE will yield 1 (one).

Convert from Floating point , when converting from floating point to integer, round towards zero . If the floating point number is outside the integer range (usually +/- 2.15e+9 = 2^31), the result is undefined because there is not enough precision for the floating point number to give an exact integer result. There is no warning in this case, not even any notification!

float data type

The word length 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 something like 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, decimal 1/3 becomes 0.3.

So never believe that the floating point number result is accurate to the last digit, and never compare whether two floating point numbers are equal. If you really need higher precision, you should use arbitrary precision math functions or the gmp function.

The above introduces the data types. PHP Section 2: Numeric type of data types, including the content of data types. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template