Created
January 12, 2017 01:53
-
-
Save merces/283a905fd9132b17ad9dc4664e6f8bc1 to your computer and use it in GitHub Desktop.
5 star rank in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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