Parsing the integer type of PHP data type (integer)

Source: Internet
Author: User
Tags integer division

An integer is a number in the collection ℤ= {..., 2,-1, 0, 1, 2, ...}.

Grammar

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

The binary representation of the integer is available from PHP 5.4.0.

To use octal expressions, the number must be preceded by 0 (0). To use hexadecimal representation, the number must be preceded by 0x. To use binary representations, the numbers must be preceded by a 0b.

Example #1 integer literal expression

<?php$a = 1234; Decimal number $ A =-123; Negative number $ A = 0123; Octal number (equal to decimal) $a = 0x1A; Hexadecimal number (equals decimal)?>

The formal description of integer integers is:

Decimal: [1-9][0-9]*
| 0

hexadecimal:0[xx][0-9a-fa-f]+

octal:0[0-7]+

binary:0b[01]+

Integer: [+-]?decimal
| [+-]?hexadecimal
| [+-]?octal
| [+-]?binary

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

Warning

If an illegal number (that is, 8 or 9) is passed to the octal number, the remaining digits are ignored.

Example #2 The odd number of octal

<?phpvar_dump (01090); Octal 010 = Decimal 8?>

Integer overflow

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

Example #3 integer Overflow under 32-bit systems

<?php$large_number = 2147483647;var_dump ($large _number);                     Int (2147483647) $large _number = 2147483648;var_dump ($large _number);                     Float (2147483648) $million = 1000000; $large _number =  50000 * $million; Var_dump ($large _number);                     Float (50000000000)?>

Example #4 integer Overflow under 64-bit systems

<?php$large_number = 9223372036854775807;var_dump ($large _number);                     Int (9223372036854775807) $large _number = 9223372036854775808;var_dump ($large _number);                     Float (9.2233720368548E+18) $million = 1000000; $large _number =  50000000000000 * $million; Var_dump ($large _number );                     Float (5.0E+19)?>

Operators that do not have an integer division in PHP. 1/2 produces a float of 0.5. The value can be cast to integer by discarding the fractional part, or better rounded with the round () function.

<?phpvar_dump (25/7);         Float (3.5714285714286) var_dump ((int) (25/7)); Int (3) var_dump (round (25/7));  Float (4)?>

Convert to integral type

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 ().

See also: discriminant of type conversions.

Converting from a Boolean value

FALSE will produce 0 (0), and TRUE will produce 1 (one).

Convert from floating-point type

When you convert from a floating-point number to an integer, the rounding is rounded down.

If the floating-point number is outside the integer range (typically +/-2.15e+9 under 32-bit platforms = +/-9.22e+18 = 2^63), the result is undefined because there is not enough precision to give an exact integer result. 2^31,64 In this case there is no warning, not even any notice!

Warning

Never cast an unknown fraction to an integer, which can sometimes lead to unpredictable results.

<?phpecho (int) ((0.1+0.7) * 10); Show 7!? >

No behavior is defined to convert from another type to an integral type. Do not rely on any existing behavior, as it will change without notice.

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.