Getting the higher integer using ceil function in PHP

float ceil ( float $input )
$input : Required : Input value in float

IN an umber we can find out the next higher integer using ceil function. This function is different than round() function. In case of round function the value is rounded to higher or lower value, but here it is always higher value is returned. Here is an example
echo ceil(2.7).'<br>'; // Output 3
echo ceil(2.4).'<br>'; // Output 3
echo ceil(2.5).'<br>'; // Output 3
echo ceil(-10.8).'<br>'; // Output -10
echo ceil(-10.3).'<br>'; // Output -10
echo ceil(-10.5).'<br>'; // Output -10
echo ceil(49/10).'<br>'; // Output  5
This function is useful when we search for a next higher hundred or thousand value of a number. For example we have a number 785 and wants to know what is the next higher hundredth number ( that is 800 ). To know this we can use ceil function. If you are generating a graph and coordinates are fixed according to high and low value of the data then using this function we can find out what should be our highest point in the graph.

//$v_max equal to 500
Same way we can find out the nearest lower value of a number using floor function in php
//$v_min equal to 400
Here is a comparison between ceil(), floor() and round() function. For different input numbers we will get different values by using these functions.
Note that there is no point is using ceil or floor functions on integers directly. ( we will get the same value )

Example of uses of ceil function

Find out number of pages required to display 10 records per page.
While displaying fixed number of records per page and giving links to navigate to different pages ( known as pagination ) we can find out the number of pages by using ceil function. The number of pages will always have the closest integer value divisible by records per page which is higher than the number of records.

We are displaying 10 records per page.
If we have 41 records then we will have 5 pages and last page will show 1 record.
If we have 49 records then we will have 5 pages and last page will show 9 records.
If we have 50 records then we will have 5 pages and last page will show 10 records.
echo ceil(49/10).'<br>'; // Output  5


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 All rights reserved worldwide Privacy Policy Disclaimer