Home > php教程 > php手册 > PHP Section 2 Numeric Data Type_php Basics

PHP Section 2 Numeric Data Type_php Basics

WBOY
Release: 2016-05-16 09:00:28
Original
1156 people have browsed it

PHP supports 8 basic data types.

Four scalar types:

  • boolean (Boolean type)
  • integer (integer type)
  • float (Floating point type, also called double)
  • string (string)
  • UL>

    Two composite types:

    • array (array)
    • object (object)

    Finally 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 boolean types, the following values ​​are considered FALSE:

    • the Boolean value FALSEitself
    • the integervalue 0 (zero)
    • the floating point value 0.0 (zero)
    • Empty String, and String "0"
    • array not containing any elements
    • Object that does not include any member variables (only applicable to PHP 4.0)
    • Special type NULL (including variables that have not been set)
    • SimpleXML object generated from an XML document without any tags (tags)

    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.

    Converting from Boolean, FALSEwill produce 0 (zero), TRUEWill produce 1 (one).

    Convert from Floating Point. When converting from floating point to integer, convert to Zerorounding. 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 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.

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template