An integer is a number of the set Z = {..., -2,
-1, 0, 1, 2, ...}.
See also: Arbitrary length
integers and Floating
point numbers
Integers can be specified in decimal (10-based),
hexadecimal (16-based) or octal (8-based) notation,
optionally preceded by a sign (- or +).
If you use the octal notation, you must precede the
number with a 0 (zero), to use
hexadecimal notation precede the number with 0x.
?php decimal : [1-9][0-9]* | 0 hexadecimal : 0[xX][0-9a-fA-F]+ octal : 0[0-7]+ integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal ? |
If you specify a number beyond the bounds of the
integer type, it will be interpreted as a float
instead. Also, if you perform an operation that results in
a number beyond the bounds of the
integer type, a float
will be returned instead.
?php $large_number = 2147483647; var_dump($large_number); // output: int(2147483647) $large_number = 2147483648; var_dump($large_number); // output: float(2147483648) // this goes also for hexadecimal specified integers: var_dump( 0x80000000 ); // output: float(2147483648) $million = 1000000; $large_number = 50000 * $million; var_dump($large_number); // output: float(50000000000) ? |
|
There is no integer division operator in PHP. 1/2 yields the float
0.5. You can cast the value to an
integer to always round it downwards, or you can use the
round() function.
?php var_dump(25/7); // float(3.5714285714286) var_dump((int) (25/7)); // int(3) var_dump(round(25/7)); // float(4) ? |
To explicitly convert a value to
integer, use either the
(int) or the (integer) cast.
However, in most cases you do not need to use the cast,
since a value will be automatically converted if an
operator, function or control structure requires an
integer argument. You can also convert a value to
integer with the function intval().
See also
type-juggling.
FALSE will yield
0 (zero), and TRUE will yield 1 (one).
When 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),
the result is undefined, since the float hasn't got
enough precision to give an exact integer result. No
warning, not even a notice will be issued in this
case!
|
|