PHP floor() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The floor() function is another in-built function in PHP iterpreter. This function accepts any float number as argument and rounds it down to the next lowest integer.

This function always returns a float number as range of float is bigger than that of integer.

Syntax

floor ( float $num ) : float

Parameters

Sr.NoParameter & Description
1num
The number to be rounded down.

Return Values

PHP floor() function returns the largest integer less than or equal to given parameter.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example rounds 15.05 to its next highest integer which is 15

<?php
   $arg=15.05;
   $val=floor($arg);
   echo "floor(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

floor(15.05) = 15

Example

 Live Demo

Following example shows that next lowest integer to 5.78 is 5.−

<?php
   $arg=5.78;
   $val=floor($arg);
   echo "floor(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

floor(5.78) = 5

Example

 Live Demo

The floor() function returns 0 for a string parameter −

<?php
   $arg="Hello";
   $val=floor($arg);
   echo "floor(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

floor(Hello) = 0

Example

 Live Demo

For negative number, it is rounded away from 0 −

<?php
   $arg=-3.95;
   $val=floor($arg);
   echo "floor(" . $arg . ") = " . $val;
?>

Output

This will produce following result −

floor(-3.95) = -4
raja
Published on 29-Jun-2020 12:21:42
Advertisements