how to calculate percentage in php (increase and decrease) from 2 values?

calculate percentage in php : Articles Get Percentage Of A Number With PHP – Use the following best PHP methods to get the percentage value of one number to another.

calculate percentage in php

percentage in php – how to calculate percentage in php?

function calculatePercentage($oldNumber, $newNumber){
    $decreaseValue = $oldNumber - $newNumber;

    return ($decreaseValue / $oldNumber) * 100;
}

how to calculate percentage in php?

index.php

";

// X = 652
// Y = 471
// % = 27.760736196319
echo calculatePercentage(652, 471);
echo "
"; // X = 47 // Y = 144 // % = -206.3829787234 echo calculatePercentage(47, 144); echo "
"; // X = 300 // Y = 320 // % = -6.6666666666667 // Note: negative as it incremented 20% echo calculatePercentage(300, 320); echo "
"; // X = 140 // Y = 140 // % = 0 // Note: no percent change echo calculatePercentage(140, 140); echo "
"; function calculatePercentage($oldNumber, $newNumber){ $decreaseValue = $oldNumber - $newNumber; return ($decreaseValue / $oldNumber) * 100; }

Get Percentage Of A Number With PHP

function calculatePercentage($total, $number)
{
  if ( $total > 0 ) {
   return round($number * ($total / 100),2);
  } else {
    return 0;
  }
}

Here are some examples of the function in action.

echo calculatePercentage(100,50).'%'; // 50%
echo calculatePercentage(100,10).'%'; // 10%
echo calculatePercentage(100,100).'%'; // 100%
echo calculatePercentage(400,3).'%'; // 0.75%
echo calculatePercentage(1234,4321).'%'; // 350.16%

How to calculate a percentage change (increase and decrease) from 2 values in PHP?

Don’t Miss : how to calculate discount
PHP: Calculate the percent difference between two numbers.


php get percentage of two numbers
Here is Best example. This time, i am showing how much it increased by:


calculate percentage between two numbers

use the function number_format:


percentage calculation in php

converts numbers into absolute numbers:


remove the decimal point completely, increase and decrease - you can use the round function:


I hope you get an idea about calculate percentage in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment