IntegersAn integer is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}. See also: SyntaxIntegers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +). Binary integer literals are available since PHP 5.4.0. To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b. Example #1 Integer literals
<?php Formally, the structure for integer literals 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 size of an integer is platform-dependent, although a maximum
value of about two billion is the usual value (that's 32 bits signed).
64-bit platforms usually have a maximum value of about 9E18, except on
Windows prior to PHP 7, where it was always 32 bit. PHP does not support
unsigned integers. Integer size can be determined
using the constant Warning
Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted. Integer overflowIf PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead. Example #2 Integer overflow on a 32-bit system
<?php Example #3 Integer overflow on a 64-bit system
<?php There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be casted to an integer to round it towards zero, or the round function provides finer control over rounding.
<?php Converting to integerTo explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval function. If a resource is converted to an integer, then the result will be the unique resource number assigned to the resource by PHP at runtime. See also Type Juggling. From booleans
From floating point numbersWhen converting from float to integer, the number will be rounded towards zero. If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms other than Windows), the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!
Warning
Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.
<?php See also the warning about float precision. From stringsFrom other typesCaution
The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice. |