Table of contents
1.
Introduction
2.
Definition and Usage  
3.
Syntax
4.
Parameters
5.
Return Value
5.1.
Example of Return Type
6.
Example
6.1.
Example 1: Basic Usage
6.2.
Example 2: Using ceil() in a Calculation
6.3.
Example 3: Using ceil() with Division
7.
Frequently Asked Questions
7.1.
What is the difference between ceil() and round() in PHP?
7.2.
Does ceil() work with negative numbers?
7.3.
What is the return type of ceil() in PHP?
8.
Conclusion
Last Updated: Feb 15, 2025
Easy

PHP ceil() Function

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The ceil() function in PHP is used to round up a floating-point number to the nearest integer. It ensures that the number always rounds up, even if the decimal part is very small. This function is useful when you need to round up prices, measurements, or calculations where rounding down is not acceptable. 

PHP ceil() Function

In this article, we will discuss the ceil() function, its syntax, parameters, return values, and examples demonstrating its usage.

Definition and Usage  

The `ceil` function in PHP is a built-in function that rounds a number up to the nearest whole number. It stands for "ceiling," which means it always moves the value upward, no matter what the decimal part is. For example, if you have a number like 4.1, the `ceil` function will round it up to 5. Similarly, even if the number is 4.99, it will still round it up to 5. This behavior makes it different from other rounding functions like `round`, which considers the decimal value before deciding whether to round up or down.

To use the `ceil` function, you don’t need to install anything extra since it’s already part of PHP. You just need to pass the number you want to round as an argument inside the function. The syntax looks like this:  

<?php
echo ceil(4.1);
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output

5


In the code above, the `ceil` function takes the number 4.1 & rounds it up to 5. The result is then displayed using the `echo` statement.  

Let’s look at another example where we work with negative numbers. When dealing with negatives, the `ceil` function still rounds "up," but "up" in this case means moving closer to zero. For example:  

<?php
echo ceil(-4.9); 
?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

-4


Here, `-4.9` is rounded up to `-4`. Even though `-4` is technically larger than `-4.9`, it’s still considered "up" because it moves closer to zero.  

The `ceil` function is commonly used in situations where you need to ensure a value is never underestimated. For example, if you’re calculating the number of pages in a pagination system & the result is 3.2, you’d want to round it up to 4 to make sure all items are included.  

Let’s take a practical example of how `ceil` can be used in such a scenario:  

<?php
$totalItems = 25; // Total number of items
$itemsPerPage = 10; // Number of items per page

// Calculate the total number of pages needed
$totalPages = ceil($totalItems / $itemsPerPage);

echo "Total Pages: " . $totalPages;
?>
You can also try this code with Online PHP Compiler
Run Code


Output: 

Total Pages: 3

In this code, we divide the total number of items (`25`) by the number of items per page (`10`). The result is `2.5`, but since we can’t have half a page, we use `ceil` to round it up to `3`. This ensures all items are accounted for without leaving any out.  

Syntax

The syntax of the ceil() function is simple and easy to use:

ceil(float $number): float

Explanation

  • ceil() takes a floating-point number as an argument.
     
  • It returns the next highest integer by rounding up.
     
  • If the number is already an integer, it remains unchanged.

Parameters

The ceil() function accepts only one parameter:

ParameterDescription
$numberThe floating-point number that needs to be rounded up.

Return Value

The function returns a floating-point number that is rounded up to the nearest integer. However, the returned value is of type float, not int.

Example of Return Type

var_dump(ceil(4.2));
var_dump(ceil(5.0)); 
You can also try this code with Online PHP Compiler
Run Code

 

Output: 

float(5)
float(5)

Example

Let's look at some examples to understand how the ceil() function works.

Example 1: Basic Usage

<?php
    echo ceil(3.2); 
    echo "<br>";
    echo ceil(7.9);
    echo "<br>";
    echo ceil(-4.3); 
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output

4
8
-4

 

Explanation:

  • ceil(3.2) returns 4 because it rounds up 3.2 to 4.
     
  • ceil(7.9) returns 8 because it rounds up 7.9 to 8.
     
  • ceil(-4.3) returns -4 because it rounds up -4.3 to -4.

Example 2: Using ceil() in a Calculation

<?php
    $price_per_item = 10.25;
    $quantity = 3;
    
    $total_cost = $price_per_item * $quantity;
    $rounded_cost = ceil($total_cost);
    
    echo "Total Cost: " . $total_cost . "<br>";
    echo "Rounded Cost: " . $rounded_cost;
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output:

Total Cost: 30.75
Rounded Cost: 31

 

Explanation:

  • ceil() ensures the total cost is rounded up to avoid undercharging.

Example 3: Using ceil() with Division

<?php
    $students = 25;
    $buses_needed = ceil($students / 4);
    
    echo "Number of buses required: " . $buses_needed;
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output:

Number of buses required: 7

 

Explanation:

  • If each bus accommodates 4 students, dividing 25 / 4 = 6.25.
     
  • The function rounds 6.25 up to 7, ensuring enough buses are allocated.

Frequently Asked Questions

What is the difference between ceil() and round() in PHP?

ceil() always rounds up to the next highest integer, while round() rounds to the nearest integer based on normal rounding rules.

Does ceil() work with negative numbers?

Yes, ceil() rounds negative numbers up towards zero. For example, ceil(-4.7) returns -4.

What is the return type of ceil() in PHP?

The ceil() function returns a float, even though it rounds the number to an integer value.

Conclusion

The PHP ceil() function is used to round a number up to the nearest integer. It always rounds a decimal value to the next highest whole number, regardless of the fraction. This function is useful when working with prices, calculations, or scenarios where upward rounding is required.

Live masterclass