Difference Between Ceil and Floor Function in PHP

Introduction

In this article I will explain Ceil() and Floor() functions in PHP. The ceil() and floor() functions are math functions. First of all I will discuss the ceil() function used to get the next highest integer value by rounding fractions up. The ceil() function is basically used for pagination.

Syntax

float Ceil($num_value);

 

Name Description
num_value The value to round

This function returns the value rounded up to the next highest integer value. It returns a rounded value of float.

Example

<?php 

$a=ceil(9.4); 

$b=ceil(29.449); 

$c=ceil(-8.4); 

$d=ceil(801.9); 

$e=ceil(1200.94); 

echo "The Ceil value of 9.4 is  ". $a . "<br />"

echo "The Ceil value of 29.449 is  ". $b . "<br />"

echo "The Ceil value of -8.4 is  ". $c . "<br />"

echo "The Ceil value of 801.9 is  ". $d . "<br />"

echo "The Ceil value of 1200.94 is  ". $e . "<br />"

?> 

Output
Ceil function in php.jpg

Next, I will discuss the floor() function. The floor function converts a specified numeric value to the next lowest integer value by rounding.

Syntax

float Floor($num_value);

 

Name Description
num_value Pass any numeric value.

Example

<?php 

$a=20; 

$b=-222.14; 

$c=200.25; 

$d=-500; 

$a=floor($a); 

$b=floor($b); 

$c=floor($c); 

$d=floor($d); 

echo "The floor value of 20 is $a<br />"

echo "The floor value of -222.14 is $b<br />"

echo "The floor value of 200.25 is $c<br />"

echo "The floor value of -500 is $d<br />"

?>

Output
Floor function in php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all