Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Variables
- $students = array(
- "Alice" => 85,
- "Bob" => 92,
- "Charlie" => 78,
- "Diana" => 90
- );
- // Function to determine the grade based on the score
- function determineGrade($score) {
- if ($score >= 90) {
- return "A";
- } elseif ($score >= 80) {
- return "B";
- } elseif ($score >= 70) {
- return "C";
- } else {
- return "F";
- }
- }
- // Array to store results
- $results = array();
- // Control structure to iterate through the students array
- foreach ($students as $name => $score) {
- // Expression to calculate the grade
- $grade = determineGrade($score);
- // Store the result in the results array
- $results[$name] = array(
- "score" => $score,
- "grade" => $grade
- );
- }
- // Output the results
- echo "<h1>Student Grades</h1>";
- echo "<table border='1'>";
- echo "<tr><th>Name</th><th>Score</th><th>Grade</th></tr>";
- foreach ($results as $name => $info) {
- echo "<tr>
- <td>$name</td>
- <td>{$info['score']}</td>
- <td>{$info['grade']}</td>
- </tr>";
- }
- echo "</table>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement