The ceil function in PHP is used to round a floating point number to the nearest integer value. When working with monetary values or other numbers that need to be rounded up to the next whole number, this function is widely utilized.
what is the syntax of the ceil function?
ceil(float $value) : float
It accepts a single parameter, the floating point number to round up, and returns a float value. Regardless of the number’s decimal value, the function always rounds up.
examples of the function
Example 1.
It takes a floating point number as an argument and returns the next highest integer value.
$value = ceil(4.3);
// returns 5
$value = ceil(-4.3);
// returns -4
In this example, an argument of 4.3 is supplied to the function, and the code returns 5, the next greatest integer. When -4.3 is supplied as an input in the second example, the method returns -4.
Example 2.
<?php
$price = $_POST['price'];
$tax = ceil($price * 0.09);
echo "The total cost is $" . ($price + $tax);
?>
The $price variable is set to the value supplied by the user in a form in this example. The tax is then calculated using the ceil() function, which multiplies the price by 0.09 and rounds up to the next largest integer number.
It’s worth mentioning that the ceil() method returns the next largest integer value but does not affect the provided argument’s original value.
Finally, the PHP ceil() function is a useful tool for rounding floating point numbers up to the next largest integer value. It comes in handy when dealing with monetary values or other numbers that must be rounded up to the next whole number. Remember that unless you reassign the result to the same variable, the ceil() method does not modify the original value of the supplied parameter.