ceil
Rounds a number up to the next whole value and always returns it as a float.
ceil(int|float $num): float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | int|float | The value to round. |
Return value
floatnum rounded up to the next highest integer, returned as a float because the float range is larger than the int range.
Errors
Throws a TypeError if num is not an int or float and cannot be coerced to one.
Changelog
- PHP 8.0.0num no longer accepts internal objects that support numeric conversion.
ceil() rounds towards positive infinity: it returns the smallest whole number that is not less than the input. ceil(4.3) is 5, and ceil(-3.14) is -3, not -4, because -3 is the larger of the two neighbours.
When to reach for it
Reach for it whenever a partial unit still costs a whole unit. Pagination is the canonical case — ceil($totalRows / $perPage) gives the page count including the short final page. The same shape covers boxes needed for an order, API calls needed for a batch size, minutes billed for a part-minute call, and rounding a price up to the next 0.25 with ceil($v / 0.25) * 0.25.
What trips people up
The return type is float even when the result is a whole number, so ceil(10 / 5) === 2 is false — it is 2.0. Cast with (int) before a strict comparison, before using it as an array index in a strict context, or before json_encode(), which would otherwise emit 5.0 where your API contract says 5.
The second trap is binary floating point. A value that displays as 2.0 may actually be 2.0000000000000004, and ceil() faithfully rounds that up to 3. When the input comes from division or repeated addition, run it through round($v, 10) first.
Example
When to use it
- Work out how many pages a result set needs: (int) ceil($total / $perPage).
- Calculate shipping boxes required when each box holds a fixed number of items.
- Round a price up to the next 25 cents or the next whole currency unit.
Frequently asked questions
Why does ceil() return a float instead of an int?
Because the float range is much larger than the int range, so returning a float means ceil() never overflows. Cast the result with (int) when you need a true integer.
What is the difference between ceil() and round()?
ceil() always rounds up towards positive infinity, so ceil(4.1) is 5. round() rounds to the nearest value, so round(4.1) is 4 and round(4.6) is 5.
How does ceil() handle negative numbers?
It rounds towards zero for negatives, because that is the direction of positive infinity: ceil(-3.14) is -3. Use floor() if you want -4.
More examples
Page count and last-page size
47 rows at 10 per page needs 5 pages, not 4, because the seven leftover rows still need somewhere to go. The cast to int is what keeps the value out of your JSON as 5 rather than 5.0.
<?php
$total = 47;
$perPage = 10;
$pages = (int) ceil($total / $perPage);
echo "Pages: $pages", PHP_EOL;
echo "Items on last page: ", $total - ($pages - 1) * $perPage, PHP_EOL;Round up to an arbitrary step
1234 rounds up to 1500 on a 500 step, and 2.03 rounds up to 2.25 on a quarter step. The last call shows a value already on the step is left alone rather than pushed to the next one.
<?php
function roundUpTo(float $value, float $step): float
{
return ceil($value / $step) * $step;
}
echo roundUpTo(1234, 500), PHP_EOL;
echo roundUpTo(2.03, 0.25), PHP_EOL;
echo roundUpTo(2.00, 0.25), PHP_EOL;
Discussion