Skip to content

Instantly share code, notes, and snippets.

@merces
Created January 12, 2017 01:53
Show Gist options
  • Save merces/283a905fd9132b17ad9dc4664e6f8bc1 to your computer and use it in GitHub Desktop.
Save merces/283a905fd9132b17ad9dc4664e6f8bc1 to your computer and use it in GitHub Desktop.
5 star rank in PHP
<?php
// Reversing From Scratch - http://menteb.in/rfs
// In this example 3 people gave 4 starts while 15 people gave 5 starts, totalizing 18 votes.
$stars = array(0, 0, 0, 3, 15);
$total = array_sum($stars);
foreach ($stars as $s)
$res += $s / $total * ++$i;
echo round($res, 1);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment