PHP section II data type numerical _php tutorial

Source: Internet
Author: User
Tags integer division scalar type null

PHP supports 8 basic types of data.

Four types of scalar:

    • Boolean (Boolean)
    • integer (integer type)
    • float (floating-point, also called double)
    • String (String)

Two kinds of composite types:

    • Array (arrays)
    • Object (objects)

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 a Boolean type, the following values are considered FALSE :

    • the Boolean value FALSE itself
    • the int value 0 (0)
    • the floating-point value 0.0 (0)
    • null string , and "0"
    • does not include any elements of
    • object (PHP 4.0 only applies)
    • special type null (including variables not yet set)
    • simplexml object
    • generated from an XML document without any tags (tags)

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

Integer data type:

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

Octal indicates that the number must be preceded by 0(0), and hexadecimal indicates that the number must be preceded by 0x.

The word length of an integer is related to the platform, although the usual maximum value is approximately 2 billion (32-bit signed). PHP does not support unsigned integers. The word length of an Integer value PHP_INT_SIZE can be expressed as a constant, since PHP 4.4.0 and PHP 5.0.5, the maximum PHP_INT_MAX value can be expressed as a constant.

If a given number exceeds the range of integers , it will be interpreted as float. Similarly, floatis returned if the result of the operation is outside the integer range.

Operators that do not have an integer division in PHP. The 0.5produces a float . You can always discard fractional parts, or use the round () function.

To explicitly convert a value to an integer, cast with (int) or (integer) . However, in most cases there is no need to cast, because when an operator, function, or process control requires an integer parameter, the value is automatically converted. You can also convert a value to an integral type by using the function intval () .

Converting from a boolean value FALSE will yield 0(0), which TRUE will produce 1(one).

When converted from floating-point numbers to integers, 0 is rounded. If the floating-point number is outside the integer range (typically +/-2.15e+9 = 2^31), the result is indeterminate because there is not enough precision to give the floating-point number an exact integer result. In this case there is no warning, not even any notice!

float data type

The length of the floating-point number is relative to the platform, although the usual maximum value is 1.8e308 and has a precision of 14-bit decimal digits (64-bit IEEE format).

It is obvious that a simple decimal score, like 0.1 or 0.7 , cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, Floor((0.1+0.7) *10 typically returns 7 instead of 8as expected because the internal representation of the result is actually similar to 7. 9.

This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0. 3.

So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. If you do need a higher precision, you should use any mathematical or GMP function of any precision.

http://www.bkjia.com/PHPjc/325306.html www.bkjia.com true http://www.bkjia.com/PHPjc/325306.html techarticle PHP supports 8 basic types of data. Four scalar types: Boolean (Boolean) integer (integer) float (floating point, also called Double) string (string) Two composite classes ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.