How can PHP be used to calculate the average of grades inputted by a user?

To calculate the average of grades inputted by a user in PHP, you can prompt the user to enter the grades, store them in an array, calculate the sum of all grades, and then divide the sum by the total number of grades to get the average.

<?php
// Prompt the user to enter grades separated by commas
$grades = readline("Enter grades separated by commas: ");

// Convert the input string into an array of grades
$grades_array = explode(",", $grades);

// Calculate the sum of all grades
$sum = array_sum($grades_array);

// Calculate the average by dividing the sum by the total number of grades
$average = $sum / count($grades_array);

// Display the average to the user
echo "The average of the grades is: " . $average;
?>