How to Use the Floor Function in PHP

How to use the floor function in php

In PHP, there are times when you may have a fraction that you don't want rounded or the nearest highest number like ceil would give you, and instead look to find the lowest next number, of which for that you'd use PHP's floor function. But as is the floor function, and how does it work? We'll explore how to round your fractions down with PHP, with easy-to-follow coding examples along the way. Let's next explore what the PHP mat floor function is.

What is PHP Math Floor?

Working with numbers, and in particular, fractions is a common practice in PHP. There may be times when you need to format numbers in a way that doesn't contain decimal places but has been rounded. You can either round up or down, but if you are looking to round down, then, PHP's floor math function is the one for you. Working in a similar by rounding up to the next highest number with ceil, the floor function does the opposite. Floor finds the next lowest integer value and returns it as a float. As the floor function expects either an integer or a float, a float is always returned, even if an int is provided.

# This is the expected behavior of floor() function
floor(int|float $num): float

How does the floor function work in PHP?

In the examples below, the floor function is returning a float back with the next lowest number. A good example is '9.99', where rounding would return 10, however, the floor function returns 9. It's also worth noting, that passing floor anything other than a float or int, will trigger a fatal error.

# Would output 4
echo floor(4.2);

# Would output 9
echo floor(9.99);

# Would output 155
echo floor(155.5);

# Would output 3
echo floor(3.00001);

# Triggers this fatal error
# Uncaught TypeError: floor(): Argument #1 ($num) must be of type int|float, array given
echo floor(['1.52']);

PHP Floor vs Ceil

Let's take a look at some examples where we are comparing floor with ceil function. Here you can see that floor returns 6, because the closest lowest whole number is 6, whereas, unlike the "round" function (which would also return 6), ceil returns 7.

# Would output 6
echo floor(6.2);

# Would output 7
echo ceil(6.2);

PHP Floor vs Round

It's common to think that rounding a number behaves the same way regardless of the rounding function used. This is not true, as all three functions behave in their own ways. The round function does behave like you might think, by rounding up or down depending on the decimal places. Here, "round" is rounding up to 4, whereas comparing this to "floor", rounds down to 3.

# Would output 3
echo floor(3.9);

# Would output 4
echo round(3.9);

Using 'number_format' with 'floor' in PHP

It's possible to combine other internal PHP functions together when using floor (or any other Math function for that matter). Using the "number_format" function with the floor function can be achieved in the example below is showing. Here we've ensured that the number is "floored" before formatting the number. Using a bigger number here is better for the example, where we want to format the number to include a comma separator at the thousands mark, but want the number to be a whole number. As have to pass the second parameter (number of decimal places) as 2 because the function by default is set to 0. Also the function's default is to include a peroid "." as the decimal separator and the comma seperator at every thousand mark and hence why we don't provide either of the third or fourth parameters.

# Would output 3,000.00
echo number_format(floor(3000.99), 2);

Conclusion

Using floor, ceil, round, and number_format are all great ways to manipulate numbers in the way you want them. Whether that be for storing in a database like MySQL or displaying on the front end of your web application. By having control and understanding how easy it is to format numbers in the way that makes your application work the way you want will make your coding journey much easier. By learning the key differeances between the different built in functions in PHP, you will know which to use next time you are working with numbers.

  • The floor function will find the next lowest whole number (oppsite to ceil)
  • Accepts both floats and integer numbers
  • Passing in anything other than floats or integers will trigger a fatal error
  • It always returns a float (when no PHP errors have been triggered)

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Related Dev Guides

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all