ceil()
Today, we will discuss the ceil() function in PHP. This function is used to round a number up to the nearest integer.
The PHP ceil() function rounds a number up to the next whole number — toward positive infinity. The name comes from "ceiling": it always returns the smallest integer value that is greater than or equal to the input. This page explains the syntax, the surprising return type, how negative numbers behave, and the practical cases where rounding up is exactly what you want (pagination, pricing, batch sizes).
Syntax
ceil(int|float $num): float$num— the value to round. PHP accepts anintorfloat; numeric strings are coerced automatically.- Return value — the next-highest whole number, returned as a
float, not anint(more on that below).
Basic example
4.2 is between 4 and 5, and ceil() always moves up, so the result is 5. Even 4.0001 rounds up to 5. A value that is already a whole number is returned unchanged.
ceil() returns a float
This trips up many beginners: ceil() returns a float, even though the value looks like an integer.
<?php
$result = ceil(4.2);
echo $result; // 5 (echo hides the .0)
var_dump($result); // float(5)
echo gettype($result); // double
?>echo displays 5 because PHP drops the trailing .0 when printing, but the underlying type is still float. If you need a true int — for example to use the value as an array key or to satisfy a strict type hint — cast it explicitly with intval() or (int):
<?php
$pages = (int) ceil(95 / 10);
var_dump($pages); // int(10)
?>How ceil() handles negative numbers
Rounding "up" means moving toward positive infinity, so for negative numbers the result is closer to zero, not further from it.
<?php
echo ceil(-4.2); // -4 (toward zero, not -5)
echo "\n";
echo ceil(-4.8); // -4
?>-4.2 rounds to -4 because -4 is larger than -4.2. This is the key difference from a naive "always add 1" — ceil() follows the number line, not the magnitude.
ceil() vs. floor() vs. round()
These three functions all reduce a number to a whole value but choose different directions:
| Function | Direction | ceil(4.2) | floor(4.2) | round(4.2) |
|---|---|---|---|---|
ceil() | Always up | 5 | — | — |
floor() | Always down | — | 4 | — |
round() | Nearest | — | — | 4 |
Use ceil() when "any leftover means one more" — floor() always discards the fraction, and round() picks the closest whole number.
Practical use cases
Pagination — how many pages do I need? If you have 95 items and show 10 per page, the last partial page still counts:
<?php
$total_items = 95;
$per_page = 10;
$total_pages = ceil($total_items / $per_page);
echo $total_pages; // 10
?>95 / 10 is 9.5; floor() would give 9 and lose the last 5 items, so ceil() is the correct choice here.
Rounding a price up to the next cent or charging in whole units works the same way — any remainder bumps you to the next unit.
Common gotchas
- It's not an integer. Compare against numbers, not types:
ceil(4.2) === 5isfalse(float vs int), whileceil(4.2) == 5istrue. Cast with(int)when you need an integer. - Floating-point precision. Because
$numis afloat, tiny representation errors can occasionally surprise you (e.g. a value that "should" be exactly5.0being stored as5.00000001). For currency, prefer integer cents over floating-point dollars. - Direction with negatives. Remember
ceil(-4.5)is-4, not-5.
Conclusion
ceil() rounds a number up to the nearest whole value and returns it as a float. Reach for it whenever a leftover fraction must "count as one more" — pagination, batch sizing, or pricing. Pair it with its siblings floor() and round(), and cast the result with intval() when you genuinely need an int.