Posted inphp

php get percentage of two numbers – 3 Ways to calculate a percentage change (increase and decrease) from 2 values in PHP

php get percentage of two numbers: Given 2 numbers old and new, where ‘new’ is incremented or decremented by some percentage of ‘old’. The task is to find out that percentage.
To calculate the percent difference between two numbers in PHP, you can use the following formula:

percent difference = |(new value - old value) / old value| * 100

php get percentage of two numbers

Calculate percentage saved between two numbers using PHP: Calculate the percent difference between two numbers.

Let’s say you have two numbers, 50 and 25.

  
  25/50*100 = 75.
  So 25 is 75% of 50.  

  50/25*100 = 133. 
  So 50 is 133% of 25. 

The percentage increase from 25 to 50 is:  
  (50-25)/25 * 100 = 33%  

The percentage decrease from 50 to 25 is:
  (50-25)/50 * 100 = 25%. 

These calculations hold true whatever your two numbers.

Don’t Miss: Calculate Percentage In Php

How to calculate percentage in PHP?

here you call the function to calculate percentage

function getCalculatePercentageChange($percentage, $of) 
{ 
    $percent = $percentage / $of; 
    return  number_format( $percent * 100, 2 ) . '%'; 
} 
 
getCalculatePercentageChange(10, 100);

PHP: Calculate the percent difference between two numbers.


php get percentage of two numbers

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

function getCalculatePercentageChange($first_vlaue, $second_value){
    $decreaseValue = $first_vlaue - $second_value;

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

PHP: Calculate the percentage difference between two numbers

function percent_diff($old, $new, $final_per = null) {
	$total_calculate = abs((1 - ($old / $new)) * 100);

	if (is_numeric($final_per)) {
		$results = $total_calculate < $final_per;

		return $results;
	}

	return $total_calculate;
}

Here's an example of how to implement this formula in PHP:

$old_value = 100;
$new_value = 150;

$percent_diff = abs(($new_value - $old_value) / $old_value) * 100;

echo "The percent difference is: " . $percent_diff . "%";

In this example, we have two variables, $old_value and $new_value, which represent the old and new values, respectively. We use the abs function to ensure that the result is always positive, regardless of whether the new value is greater than or less than the old value.

The result of the calculation is stored in the $percent_diff variable, which is then printed to the screen using the echo statement.

PHP calculate percentage of total

To calculate the percentage of total using PHP, you can use the following formula:

$percentage = ($value / $total) * 100;

In this formula, $value represents the value for which you want to calculate the percentage, and $total represents the total value that $value is a part of.

For example, let's say you have an array of sales values and you want to calculate the percentage of each value out of the total sales. You can use the following PHP code:

$sales = array(100, 200, 300, 400);
$total_sales = array_sum($sales);

foreach($sales as $value) {
    $percentage = ($value / $total_sales) * 100;
    echo $value . " is " . $percentage . "% of total sales
"; }

In this code, we first calculate the total sales by using the array_sum() function. Then, we loop through the sales array and calculate the percentage of each value out of the total sales. Finally, we print out the result using the echo statement.
You can replace the values of $old_value and $new_value with your own values to calculate the percent difference between any two numbers.

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

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype