floor

Rounds a number down to the nearest integer and returns it as a float.

Signature
floor(int|float $num): float
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$numint|floatThe numeric value to round down.

Return value

floatThe next lowest integer value, always returned as a float even though it has no fractional part.

Errors

Throws a TypeError if $num is not an int, a float, or a numeric string.

Changelog

  • PHP 8.0.0$num no longer accepts internal objects that support numeric conversion; passing one now raises a TypeError.

floor() rounds toward negative infinity: it returns the largest integer that is not greater than $num. For positive numbers this looks like chopping off the decimals, but for negative numbers it does not — floor(-3.14) is -4, not -3.

When to reach for it

Pagination and bucketing are the classic uses: how many complete pages of 10 fit in 47 items, which 5-minute slot a timestamp belongs to, how many whole units of something you can afford. Anywhere you need a lower bound rather than nearest rounding.

What trips people up

The return type is float, not int. That surprises people using strict types, array keys or === comparisons: floor(4.3) === 4 is false because the left side is 4.0. Cast with (int) when you need a real integer, and remember that for values beyond PHP_INT_MAX that cast is undefined.

The bigger trap is float precision. 79.99 * 100 is stored as 7998.999999999999, so floor() gives 7998 rather than the 7999 you expected. Money should be held as integer minor units, or run through round() before floor() if you have to work in floats.

For truncating toward zero regardless of sign, use (int) or intdiv() instead — those are not the same operation as floor().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Work out how many complete pages of results a total item count produces.
  • Snap a Unix timestamp down to the start of its 5-minute or hourly bucket.
  • Calculate how many whole units a budget covers at a given unit price.

Frequently asked questions

Why does floor return a float instead of an int?

floor() is defined over the float range, which is wider than PHP's int range, so it returns float to avoid overflow. Cast with (int) floor($n) when you need a true integer.

What is the difference between floor and the (int) cast?

They agree for positive numbers but not negative ones: floor(-2.3) is -3 while (int) -2.3 is -2. floor() rounds toward negative infinity, the cast truncates toward zero.

Why does floor(79.99 * 100) return 7998?

79.99 cannot be stored exactly in binary floating point, so the product is 7998.999999999999 and floor() correctly rounds that down. Store money as integer minor units, or apply round() before floor().

More examples

floor, ceil, round and int cast compared

For positive numbers floor() and the (int) cast agree, but at -2.3 floor gives -3 while (int) gives -2 — the cast truncates toward zero, floor goes toward negative infinity. round() moves to the nearest value and breaks ties away from zero, so -2.5 becomes -3.

Example · php
<?php
printf("%-8s %-7s %-7s %-7s %-7s\n", 'value', 'floor', 'ceil', 'round', '(int)');
foreach ([2.7, 2.5, 2.3, -2.3, -2.5, -2.7] as $n) {
    printf("%-8s %-7s %-7s %-7s %-7s\n",
        $n, floor($n), ceil($n), round($n), (int) $n);
}

Bucketing timestamps into 5-minute slots

Dividing by 300 and flooring snaps every timestamp back to the start of its 5-minute window, so 00:07:35 and 00:11:39 land in different buckets while values inside one window collapse together. The (int) cast is needed because floor() hands back a float.

Example · php
<?php
$times = [1735689900, 1735690055, 1735690299, 1735690301];
foreach ($times as $t) {
    $slot = (int) floor($t / 300) * 300;
    printf("%s -> slot %s\n", gmdate('H:i:s', $t), gmdate('H:i:s', $slot));
}

Pagination without off-by-one errors

floor() counts the 4 complete pages while ceil() gives the 5 pages actually needed, including the short last one. Mixing the two up is the classic pagination off-by-one: the final 7 items would never be shown.

Example · php
<?php
$total = 47; $perPage = 10;
$fullPages  = (int) floor($total / $perPage);
$totalPages = (int) ceil($total / $perPage);

echo "full pages:  $fullPages\n";
echo "total pages: $totalPages\n";
echo "last page holds: ", $total - $fullPages * $perPage, " items\n";

Discussion

  • Be the first to comment on this lesson.