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

<?php

// X = 800
// Y = 400
// % = 50
echo calculatePercentage(800, 400);
echo "<br/>";

// X = 652
// Y = 471
// % = 27.760736196319
echo calculatePercentage(652, 471);
echo "<br/>";

// X = 47
// Y = 144
// % = -206.3829787234
echo calculatePercentage(47, 144);
echo "<br/>";

// X = 300
// Y = 320
// % = -6.6666666666667
// Note: negative as it incremented 20%
echo calculatePercentage(300, 320);
echo "<br/>";

// X = 140
// Y = 140
// % = 0
// Note: no percent change
echo calculatePercentage(140, 140);
echo "<br/>";

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.

Read Also:  Redirecting a Web Folder Directory to another Directory in htaccess

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

$oldSalesItem = 14;
$newSalesItem = 12.50;

$calculatePercentage = (1 - $oldSalesItem / $newSalesItem) * 100;

echo $calculatePercentage;

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

Read Also:  Date Range Search Jquery DatePicker using Ajax PHP with MySQL

<?php

$oldSalesItem = 14;
$newSalesItem = 17;

$calculatePercentage = (1 - $oldSalesItem / $newSalesItem) * 100;

echo $calculatePercentage;

calculate percentage between two numbers

use the function number_format:

<?php

$oldPrice = 14;
$newPrice = 17;

$calculatePercentage = (1 - $oldPrice / $newPrice) * 100;

echo number_format($calculatePercentage, 2);

percentage calculation in php

converts numbers into absolute numbers:

<?php

$oldSalesItem = 14;
$newSalesItem = 10;

$calculatePercentage = (1 - $oldSalesItem / $newSalesItem) * 100;

echo abs($calculatePercentage);

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

<?php

$oldSalesItem = 14;
$newSalesItem = 17;

$calculatePercentage = (1 - $oldSalesItem / $newSalesItem) * 100;

echo round($calculatePercentage, 0);

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.