Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
PHP floor() Function
The floor() function is a built-in PHP function that accepts any float number as argument and rounds it down to the next lowest integer. This function always returns a float number as the range of float is bigger than that of integer.
Syntax
floor ( float $num ) : float
Parameters
| Parameter | Description |
|---|---|
| num | The number to be rounded down |
Return Value
The floor() function returns the largest integer less than or equal to the given parameter as a float value.
Examples
Basic Usage
Here's a simple example demonstrating the floor() function with a negative number −
<?php
$arg = -3.95;
$val = floor($arg);
echo "floor(" . $arg . ") = " . $val;
?>
floor(-3.95) = -4
Multiple Examples
Let's see how floor() works with different types of numbers −
<?php
echo "floor(4.7) = " . floor(4.7) . "
";
echo "floor(4.2) = " . floor(4.2) . "
";
echo "floor(-2.1) = " . floor(-2.1) . "
";
echo "floor(-2.9) = " . floor(-2.9) . "
";
echo "floor(5) = " . floor(5) . "
";
?>
floor(4.7) = 4 floor(4.2) = 4 floor(-2.1) = -3 floor(-2.9) = -3 floor(5) = 5
Key Points
- For positive numbers,
floor()removes the decimal part - For negative numbers, it rounds toward negative infinity
- Always returns a float, even when the input is an integer
- Available in PHP 4.x, 5.x, and 7.x versions
Conclusion
The floor() function is useful for rounding numbers down to the nearest integer. Remember that it always rounds toward negative infinity, which affects how negative numbers are handled.
Advertisements
