Getting the higher integer using ceil function in PHP

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=485;
$base_m=100;
$v_max=$base_m*(ceil($v/$base_m));
//$v_max equal to 500
Same way we can find out the nearest lower value of a number using floor function in php
$v=485;
$base_m=100;
$v_min=$base_m*(floor($v/$base_m));
//$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.
ValueFloor()ceil()round()
3.7344
3.4343
3.5344
3.48343
3.62344
2222
-8.7-9-8-9
-9.5-10-9-10
-9.3-10-9-9
-9.49-10-9-9
-9.61-10-9-10
-9-9-9-9
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.

Example.
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

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Google+

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer