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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentPHPfloor()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

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.