How to rank a quiz PHP


We often need rankings in the quiz system. So this example is all about how to rank a multiple number of scores or results.

index.php

 <?php
$numbers = array( 101, 201, 301, 301, 401, 401, 401, 501, 501, 501, 501);
rsort($numbers);

$arrlength = count($numbers);
$rank = 1;
$prev_rank = $rank;

for($x = 0; $x < $arrlength; $x++) {

    if ($x==0) {
         echo $numbers[$x]."- Rank".($rank);
    }

   elseif ($numbers[$x] != $numbers[$x-1]) {
        $rank++;
        $prev_rank = $rank;
        echo $numbers[$x]."- Rank".($rank);
   }

   else{
        $rank++;
        echo $numbers[$x]."- Rank".($prev_rank);
    }

   echo "<br>";
}
?>