I read the manual from the php homepage
It writes:
// this doesn't go for
//hexadecimal specified integers above 2^32-1:
var_dump( 0x100000000 );
// output: int(2147483647)
But it has 4.5 bytes which is larger than int(4 bytes), and I test it.
It outputs float.
I don't understand why they contradict?
2 Answers 2
From the PHP manual page on integers: "If 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."
Since the integer type is too small to represent that number, PHP is automagically converting it to a float so you don't lose data. This is expected behavior.
However, the specific example that you quoted is clearly wrong in the manual. It looks like someone made an error when writing the manual, or it may be that the behavior of oversize hexadecimal literals was changed since the time that manual page was written.
Comments
It outputs a float for me: float(4294967296)
echo PHP_INT_MAX;tell you?