Working with numbers is a common task any PHP programmer will do at some point, but do you know what the different mathematical functions in PHP do, such as 'ceil', 'floor', and 'round'? Getting a good understanding of these functions is critical to working with numbers in PHP and overcoming challenges such as rounding a number to the nearest whole number. We've put together this guide to help you understand how to answer that question, along with really simple-to-follow PHP coding examples and we'll explore some key differences with ceil vs floor and round.
PHP offers dozens of math functions all with their own purpose, but in order to round a number to the nearest whole number, regardless of the number of decimal places that number might have, the best function for that would be PHP's ceil method. Let's explore how it works, along with PHP code to show you the way.
How does the ceil function work In PHP?
The ceil function in PHP, available since PHP 4 and still available to use in the latest versions of PHP, including PHP 8+, allows you to round fractions up to the nearest highest integer value. Expecting either an integer or a float will round that roundup and return a float back. If the number doesn't need rounding, then the same number will be passed back. This is still the case even if you pass the function an integer, a float is returned.
# This is the expected behavior of ceil() function
ceil(int|float $num): float
How to round up to the nearest whole number?
There may be times in your web application when you need to round numbers, maybe when showing numbers on the front end, or you don't need a number to contain decimal places and want the highest available number from the given number. To round to the nearest whole number we can use ceil. Let's take a look at a few examples of how ceil works in PHP.
# Would output 6
echo ceil(5.9);
# Would output 6
echo ceil(5.94);
# Would output 6
echo ceil(5.1);
# Would output 6
echo ceil(5.01);
The same behavior exists when "ceiling" a negative number, as the following example shows.
# Would output -99
echo ceil(-99.99);
PHP Ceil vs Round
What is the difference between PHP's ceil and round functions? This a common question, especially when we're attempting to round a number. Both functions might appear to behave the same way when given certain numbers, but they behave in slightly different ways. Let's take a look at an example where ceil and round appear to do the same job.
# Would output 10
echo ceil(9.9);
# Would output 10
echo round(9.9);
However, let's pass a number where the decimal is lower than 5 to see what's returned. Here you can see they return different numbers. Ceil as expected returned the highest whole number, which is 4, not 3, and round, has rounded down to 3 because this is the expected behavior of round. Unlike ceil, round returns the nearest whole number and not the highest or lowest.
# Would output 4
echo ceil(3.4);
# Would output 3
echo round(3.4);
What's the difference between Ceil and Floor
Another common question with PHP's maths functions is, what is the main difference between the function ceil and floor? The key difference with the floor function is it will return the lowest number, whereas ceil is attempting to return the next highest.
# Would output 9
echo ceil(8.5);
# Would output 8
echo floor(8.5);
Ceil PI in PHP
One interesting aspect of ceil in PHP is attempting to return the next highest number of Pi. PHP even has its own Pi function which we can use, which is even better as we know Pi is infinite. Using the "pi()" function, let's try all three functions to see what we get. We should have "ceil" go to the highest next number, which is 4, "floor" should drop to the lowest whole number which is 3, and finally "round" should round drop as the first decimal point is less than 5.
# Would output 3.14159265358984
echo pi();
# Would output 4
echo ceil(pi());
# Would output 3
echo floor(pi());
# Would output 3
echo round(pi());
When you may use ceil in your PHP application
There are many use cases for ceil in modern-day PHP applications. To help showcase the ceil function in PHP here are a few real-world examples of it in action.
- The ceil function can be highly useful (and is very common) in pagination. When querying results and you wish to split those results into pages, say 10 per item, but only have 27, you can't have 2.7 of a page, instead you'll have 3. Using ceil will help you display on your website that 3 pages are required
- When working with prices, and price calculations, you may wish to round up the numbers to display and use to your end user, to ensure the customer pays the correct
- Handling progress amount or resources, you may wish to round up to keep a consistent number instead of displaying many decimal point numbers
Conclusion
Ceil is a great way to find the highest next integer of a float that you provide. Combining this with your knowledge of the floor and round functions will be a great stepping stone to working with maths in PHP.
- Use the ceil function to round a number up
- Use the floor function to round a number down
- Use the round function to round a number down if the decimal point is less than 5 and up if 5 or greater.