PHP: Calculate the percentage of a number.

This is a short guide on how to calculate the percentage of a given number in PHP. For example: What is 25% of 100? Or what is 10% of 728?

In this code snippet, I have put each calculation on its own separate line, just to make the math a little clearer:

A quick summary of what we did:

  1. We used the number 928 for example purposes.
  2. We specified that we wanted to get 25% of 928.
  3. We converted our percentage value into a decimal number by dividing it by 100. This turns 25 into 0.25.
  4. We then multipliedĀ 0.25 by 928.
  5. The end result is 232, which means that 25% of 928 is 232.

Function.

If you’re looking for a simple PHP function to use in your projects, then you can use the following:

I’m using the same example figures, just to demonstrate that the result is the same. As you can see, theĀ calculation can be done with one simple line of code.

Related articles:

Comments

comments