floor() function in PHP

The floor() function rounds a number down to the nearest integer. It always rounds towards negative infinity, which means positive decimals are rounded down and negative decimals are rounded further away from zero.

Syntax

floor(num)

Parameters

  • num − The number to round down

Return Value

The floor() function returns the value rounded down to the nearest integer as a float.

Example 1

Basic usage with positive decimal numbers −

<?php
    echo(floor(0.10) . "<br>");
    echo(floor(0.55) . "<br>");
    echo(floor(9.99));
?>
0
0
9

Example 2

Using floor() with whole numbers −

<?php
    echo(floor(9));
?>
9

Example 3

Behavior with negative numbers −

<?php
    echo(floor(7.8) . "<br>");
    echo(floor(-4.2) . "<br>");
    echo(floor(-1.1));
?>
7
-5
-2

Key Points

  • For positive numbers, floor() removes the decimal part

  • For negative numbers, it rounds further away from zero

  • The function returns a float value, not an integer

  • Whole numbers remain unchanged

Conclusion

The floor() function is useful for rounding numbers down to the nearest integer. Remember that it always rounds towards negative infinity, which affects how negative numbers are handled.

Updated on: 2026-03-15T07:27:27+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements