How can the issue of always displaying a rank of 1 be resolved in the code snippet?

The issue of always displaying a rank of 1 can be resolved by properly incrementing the rank variable inside the foreach loop. Each time a new student is processed, the rank should be incremented by 1 to reflect their position in the ranking. This ensures that each student is assigned a unique rank based on their score.

$rank = 1; // Initialize rank variable outside of the loop

foreach ($students as $student) {
    // Compare student's score with previous student's score to determine rank
    if ($student['score'] < $prev_score) {
        $rank++; // Increment rank if current student has lower score
    }

    echo "Rank: " . $rank . " - Name: " . $student['name'] . " - Score: " . $student['score'] . "<br>";

    $prev_score = $student['score']; // Update previous score for next iteration
}