Tag Archives: geeks

A weird PHP behavior

This post about the oddity on how PHP converts float to integer. This is not new to most of the PHP geeks out there, but I just want to point this out to those who are not aware of this yet. You may or may not tackle this in the future, but it’s worth knowing about.

See the PHP code below:

echo (int)((0.7 + 0.1) * 10);

At first look, what’ll come to your mind is that the output will be ’8′. But that’s not the actual output. If you’ll try to run it – the output is ’7′.
That’s not all, try:

echo (int)((0.6 + 0.1) * 10);  //outputs 7
echo (int)((0.8 + 0.1) * 10); //outputs 9

See the result? Okay, so that sums it up. ;)