The ceil() function in PHP is used to round a number up to the nearest integer. It always rounds fractions upward, regardless of whether the fractional part is less than 0.5.
Syntax
ceil(float $value): float
Key Points:
- Returns the next highest integer.
- The return type is still float, not int.
- It works with both positive and negative numbers.
Examples:
echo ceil(4.3); // Output: 5
echo ceil(9.999); // Output: 10
echo ceil(-3.2); // Output: -3
echo ceil(-7.8); // Output: -7
echo ceil(5); // Output: 5 (already an integer)
Use Case Example:
Imagine you're calculating the number of pages needed to show items in pagination, where each page shows 10 items:
$totalItems = 53;
$itemsPerPage = 10;
$pages = ceil($totalItems / $itemsPerPage); // 53 / 10 = 5.3 → ceil(5.3) = 6
echo $pages; // Output: 6