php percentage – how to get percentage of value in php ?

php percentage calculating a percentage is just basic math Calculate the percentage of a number. Use the following Examples to find the percentage value of one number to another.

php percentage

PHP: Calculate the percentage of a number.

PHP calculate percentage of total

PHP program to calculate percentage

<?php
 function getCalcPer($number_digit, $num_total) {
  $results1 = $number_digit / $num_total;
  $results2 = $results1 * 100;
  $total = number_format($results2, 0);
  return $total;
}

echo "Results of 39 in 100 : ".getCalcPer(39, 100).'%<hr/>';
echo "Results of 26 in 200 : ".getCalcPer(26, 200).'%<hr/>';
echo "Results of 17 in 150 : ".getCalcPer(17, 150).'%<hr/>';
echo "Results of 30 in 400 : ".getCalcPer(30, 400).'%<hr/>';
echo "Results of 11 in 190 : ".getCalcPer(11, 190).'%<hr/>';
?>

Output of the above code –

Results of 39 in 100 : 39%
Results of 26 in 200 : 13%
Results of 17 in 150 : 11%
Results of 30 in 400 : 8%
Results of 11 in 190 : 6%

Example

<?php

//Our number is 928.
$myNumber = 928;

//We want to get 25% of 928.
$percentToGet = 25;

//here simple Convert our percentage value into a decimal format.
$percentInDecimal = $percentToGet / 100;

//Get the result.
$percent = $percentInDecimal * $myNumber;

//Print it out - Result is 232.
echo $percent;

don’t Miss : calculate percentage in php

Read Also:  PHP MySQL CRUD Create, Insert, Update and Delete operations

Get Percentage Of A Number With PHP

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

sample examples of the function in action.
echo getCalcPer(100,50).'%'; // 50%
echo getCalcPer(100,10).'%'; // 10%
echo getCalcPer(100,100).'%'; // 100%
echo getCalcPer(400,3).'%'; // 0.75%
echo getCalcPer(1234,4321).'%'; // 350.16%

I hope you get an idea about php percentage.
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.