Advertisement
vvccs

wt_7yphparray

Oct 13th, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. <?php
  2. // Variables
  3. $students = array(
  4.     "Alice" => 85,
  5.     "Bob" => 92,
  6.     "Charlie" => 78,
  7.     "Diana" => 90
  8. );
  9.  
  10. // Function to determine the grade based on the score
  11. function determineGrade($score) {
  12.     if ($score >= 90) {
  13.         return "A";
  14.     } elseif ($score >= 80) {
  15.         return "B";
  16.     } elseif ($score >= 70) {
  17.         return "C";
  18.     } else {
  19.         return "F";
  20.     }
  21. }
  22.  
  23. // Array to store results
  24. $results = array();
  25.  
  26. // Control structure to iterate through the students array
  27. foreach ($students as $name => $score) {
  28.     // Expression to calculate the grade
  29.     $grade = determineGrade($score);
  30.    
  31.     // Store the result in the results array
  32.     $results[$name] = array(
  33.         "score" => $score,
  34.         "grade" => $grade
  35.     );
  36. }
  37.  
  38. // Output the results
  39. echo "<h1>Student Grades</h1>";
  40. echo "<table border='1'>";
  41. echo "<tr><th>Name</th><th>Score</th><th>Grade</th></tr>";
  42.  
  43. foreach ($results as $name => $info) {
  44.     echo "<tr>
  45.            <td>$name</td>
  46.            <td>{$info['score']}</td>
  47.            <td>{$info['grade']}</td>
  48.          </tr>";
  49. }
  50.  
  51. echo "</table>";
  52. ?>
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement