PHP floor() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The floor() function rounds a number down to the nearest whole number.

It always moves toward the smaller number, so 4.9 becomes 4 and -4.1 becomes -5.

Syntax

Call floor() with the values you need. Start with this line.

PHP
You pass a number to floor() and it returns the next whole number that is less than or equal to it.
CallWhat it does
floor(4.7)Returns 4.
floor(-4.2)Returns -5, because -5 is lower than -4.2.
floor(9)Returns 9, since it is already whole.

Use ceil() to round up and round() to round to the nearest number.

Library declarations
PHP
floor(int|float $num): float

Parameters

ParameterDescription
numRequired. The number you want to round down. It can be an int or a float.

Return Value

Return ValueDescription
floatThe largest whole number that is not greater than num, returned as a float.

PHP Examples

Example 1 — Round numbers down

See how floor() cuts off the decimal part of positive numbers.

PHP
<?php
echo floor(4.7) . PHP_EOL;
echo floor(4.2) . PHP_EOL;
echo floor(9) . PHP_EOL;

How It Works

  1. floor(4.7) goes down to 4, not up to 5.
  2. floor(4.2) also goes down to 4.
  3. floor(9) stays 9 because there is nothing to round.
  4. The values are floats, but PHP prints them without .0.

Example 2 — Negative numbers and the return type

Negative numbers behave differently from what many beginners expect.

PHP
<?php
echo floor(-4.2) . PHP_EOL;
echo floor(-0.5) . PHP_EOL;
echo gettype(floor(5)) . PHP_EOL;

How It Works

  1. floor(-4.2) returns -5 because -5 is the next lower whole number.
  2. floor(-0.5) returns -1, not 0.
  3. gettype() reports "double", which is PHP's name for float.
  4. So even floor(5) gives a float and not an int.

Example 3 — Count full boxes

Use floor() to find how many complete groups fit into a total.

PHP
<?php
$items = 47;
$perBox = 12;

$fullBoxes = floor($items / $perBox);
$leftOver = $items % $perBox;

echo "Full boxes: " . $fullBoxes . PHP_EOL;
echo "Left over: " . $leftOver . PHP_EOL;

How It Works

  1. 47 divided by 12 is about 3.9167.
  2. floor() drops the decimal part, leaving 3 full boxes.
  3. The % operator gives the remainder, which is 11 items.
  4. Together they show how the items are packed.

Frequently Asked Questions

It rounds a number down to the nearest whole number and returns it as a float.
floor() always goes down. round() goes to the nearest whole number, so round(4.7) is 5 but floor(4.7) is 4.
It goes toward the lower number, so floor(-4.2) returns -5.
No, it returns a float. Use (int) floor($x) or intdiv() when you need an integer.
Use ceil(). For example ceil(4.2) returns 5.

Next: fmod()

The fmod() function returns the remainder after dividing one number by another, and it wor.

fmod() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful