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.
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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.