Backend Development 3 min read

Using PHP floor() Function: Syntax, Examples, and Tips

This article explains PHP's floor() function, its syntax, provides multiple code examples for rounding floats, integers, negative numbers, and arrays, and highlights important notes such as its difference from round() and its combination with other math functions.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP floor() Function: Syntax, Examples, and Tips

The PHP floor() function rounds a floating‑point number down to the nearest integer, returning the greatest integer less than or equal to the given value; if the argument is already an integer, it is returned unchanged.

Syntax: floor($number) , where $number is the float or integer to be rounded down.

Examples:

• Rounding a float: $number = 3.14; $result = floor($number); echo $result; // 3

• Rounding an integer: $number = 5; $result = floor($number); echo $result; // 5

• Rounding a negative number: $number = -2.5; $result = floor($number); echo $result; // -3

• Using floor() with array_map to process an array: $numbers = [2.3, 4.7, 6.1]; $result = array_map('floor', $numbers); print_r($result); // Array ( [0] => 2 [1] => 4 [2] => 6 )

Notes: floor() only truncates toward negative infinity and does not perform conventional rounding; for rounding use round() . It can be combined with other math functions such as ceil() and fmod() .

In summary, the floor() function is a useful tool for numerical calculations and data processing in PHP.

Backend DevelopmentphpCode Examplefloor()math functions
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.