Get Percentage – PHP Function

Get Percentage - PHP FunctionHere is an incredibly short, sweet and simple php function that I use all of the time.  This function provides you with a rounded percentage, base between two numbers – the number of instances and the total number. With these two numbers, a percentage can easily be established. It’s not record science and if you are new to PHP, pour through the following PHP function and see if you can work out how it works.

Build Get Percentage function

<?php
// Get percentage function
function Get_Percentage($number,$total)
{
 if($total > 0)
 {
  $percentage = round($number * 100 / $total)."%"; // Get percentage
  echo $percentage;
 }
}

// Usage
echo "The percentage of people who came to my party was ".Get_Percentage(19,23);
?>

For example, if I invited 23 friends to a party and only 19 turned up, to get the percentage I would use Get_Percentage(19,23). This would parse as ‘83%’ – phew, I still have the majority in the real friends stake!

Leave a Reply

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