Integer overflow
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)
?>
Warning
Unfortunately, there was a bug in PHP so that this does
not always work correctly when there are negative numbers
involved. For example: when you do -50000 * $million, the
result will be -429496728. However, when both operands are
positive there is no problem.
This is solved in PHP 4.1.0.
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)
?>
|