How to calculate percentage between two numbers in PHP

1 Answer

0 votes
function GetPercentage($num1, $num2) {
    return ($num1 / $num2) * 100.0;
}
 
echo "30 is " . GetPercentage(30, 40) . "% of 40" . "\n";
  
echo "40 is " . GetPercentage(40, 30) . "% of 30" . "\n";
  
echo "20 is " . GetPercentage(20, 35) . "% of 35" . "\n";
echo "20 is " . number_format((float)GetPercentage(20, 35), 2, '.', '')  . "% of 35";

  
  
 
  
/*
run:
  
30 is 75% of 40
40 is 133.33333333333% of 30
20 is 57.142857142857% of 35
  
*/

 



answered May 23, 2022 by avibootz

Related questions

...