PHP ceil() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP ceil() function rounds a number up to the next whole number.

Even a tiny decimal part such as 4.01 is rounded up, so the result is always greater than or equal to the original number.

Syntax

Call ceil() with the values you need. Start with this line.

PHP
$result = ceil($num);
CallWhat it does
ceil(4.1)Returns 5 because 4.1 is rounded up.
ceil(4.9)Returns 5 as well.
ceil(-4.9)Returns -4 because rounding up moves toward zero for negatives.

ceil() always returns a float, even though the value looks like a whole number. Cast with (int) if you need an integer.

Library declarations
PHP
ceil(int|float $num): float

Parameters

ParameterDescription
numRequired. The number you want to round up.

Return Value

Return ValueDescription
floatThe smallest whole number that is greater than or equal to $num, returned as a float.

PHP Examples

Example 1 — Basic ceil() usage

This example rounds up positive and negative decimals and also shows that a whole number stays the same.

PHP
<?php
echo ceil(4.1) . PHP_EOL;
echo ceil(4.9) . PHP_EOL;
echo ceil(-4.1) . PHP_EOL;
echo ceil(5) . PHP_EOL;

How It Works

  1. ceil(4.1) moves up to the next whole number, 5.
  2. ceil(4.9) also becomes 5.
  3. ceil(-4.1) rounds up toward zero, so the result is -4.
  4. ceil(5) is already whole, so it stays 5.

Example 2 — Return type and negative zero

ceil() always returns a float. This example uses var_dump() to see the real type and a special case with negative zero.

PHP
<?php
var_dump(ceil(4));
var_dump(ceil(-0.5));
var_dump((int) ceil(4.1));

How It Works

  1. ceil(4) receives an int, but the result is still float(4).
  2. ceil(-0.5) rounds up to zero, but as a negative number it becomes float(-0).
  3. (int) ceil(4.1) first rounds 4.1 up to 5.0.
  4. The (int) cast then turns the float into int(5).

Example 3 — Calculate the number of pages

ceil() is perfect for pagination. If you have 23 items and show 10 per page, you need 3 pages, not 2.3.

PHP
<?php
$totalItems = 23;
$perPage = 10;

$pages = (int) ceil($totalItems / $perPage);
echo "Total pages: $pages" . PHP_EOL;

$price = 4.231;
echo ceil($price * 100) / 100 . PHP_EOL;

How It Works

  1. 23 / 10 gives 2.3, which means we need more than 2 pages.
  2. ceil(2.3) rounds up to 3, and (int) turns it into a normal integer.
  3. To round up to 2 decimals we multiply 4.231 by 100 to get 423.1.
  4. ceil(423.1) is 424, and dividing by 100 gives 4.24.

Frequently Asked Questions

ceil() rounds a number up to the next whole number. For example, ceil(4.1) returns 5.
No. It always returns a float. Use (int) ceil($num) if you need an integer.
It still rounds up, which for negatives means toward zero. ceil(-4.9) returns -4.
ceil() always goes up. round() goes to the nearest whole number, so round(4.1) is 4 but ceil(4.1) is 5.
Use floor(). It is the opposite of ceil() and always rounds down, so floor(4.9) is 4.

Next: clamp()

clamp() function keeps a value inside a range. If the value is below the minimum it return.

clamp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful