Welcome to our comprehensive guide on the PHP ceil() function! In this lesson, we'll explore what ceil() does, why we use it, and how to use it in your PHP projects. Let's get started! π
The ceil() function in PHP is a built-in function that rounds a given number up to the nearest integer. It's useful when you want to ensure that a floating-point number is rounded up to the nearest whole number.
The syntax for the ceil() function is simple:
ceil($number);Replace $number with the floating-point value you want to round up.
<?php
$number = 6.3;
$rounded = ceil($number);
echo "The rounded number is: $rounded";
?>
// Output: The rounded number is: 7In this example, we have a floating-point number 6.3. By using the ceil() function, we round it up to the nearest integer, which is 7.
<?php
$price = 34.56;
$discount = 20;
$discount_amount = ($price * $discount) / 100;
$final_price = ceil($price - $discount_amount);
echo "The final price after discount is: $final_price";
?>
// Output: The final price after discount is: 27In this example, we calculate a discount on a product price and then round the final price up to the nearest integer.
What does the PHP `ceil()` function do?
Stay tuned for our next lesson where we'll explore the floor() function, its uses, and examples! π