Integers

Integers can be specified using any of the following syntaxes:


$a = 1234; # decimal number
$a = -123; # a negative number
$a = 0123; # octal number (equivalent to 83 decimal)
$a = 0x12; # hexadecimal number (equivalent to 18 decimal)
     

The size of an integer is platform-dependent, although a maximum value of about 2 billion is the usual value (that's 32 bits signed).

Integer overflow

Because of the flexible type-juggling, a number is autmatically converted to float when it is about to overflow.

Converting to integer

See type-juggling for general information about converting.

From booleans

False will yield 0 (zero), and True will yield 1 (one).

From floating point numbers

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.

Warning

No warning, not even a notice will be issued in this case!

Warning

Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.


echo (int) ( (0.1+0.7) * 10 ); // echo's 7!
        

See for more information the warning about float-precision.