浮点数和零比较

I have a simple PHP task, to sum all number's digits.

$number = 345;
$digit = $number;
$sum = 0;

while ($number > 0) {
  $digit = $number % 10;
  $sum += $digit;

  $number /= 10;
}

This solution would give correct result. However, I'm aware that it will enter loop way more than three times. And eventually it will become equal to zero.

Why is that happening? At what time, floats become zero? Just by following math principles, this would be an infinite loop, right? And since there are more than 3, 4 and 5 digits, how end result is not greater than 12 (even for that little amount).

P.S. I know that I should solve this by rounding $number value for example, but I'm just curios about floats and its behaviour.

When you update number you should really be doing this

$number -= $digit;
$number /= 10;

Floats are platform specific, check out this link http://php.net/manual/en/language.types.float.php