How can PHP developers efficiently calculate and display survey results as percentages based on user votes?

To efficiently calculate and display survey results as percentages based on user votes, PHP developers can first tally the total number of votes for each option, then calculate the percentage of votes each option received in relation to the total number of votes. Finally, they can display the results in a visually appealing format to the users.

// Sample array containing survey results
$surveyResults = [
    'Option A' => 30,
    'Option B' => 45,
    'Option C' => 25
];

// Calculate total number of votes
$totalVotes = array_sum($surveyResults);

// Loop through each option to calculate and display percentage
foreach ($surveyResults as $option => $votes) {
    $percentage = ($votes / $totalVotes) * 100;
    echo $option . ': ' . $percentage . '%<br>';
}