Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: romain
  5. * Date: 22/02/16
  6. * Time: 10:13
  7. */
  8.  
  9. $students = [
  10. "Emmanuel" => 42,
  11. "Thierry" => 51,
  12. "Pascal" => 45,
  13. "Eric" => 48,
  14. "Nicolas" => 19
  15. ];
  16.  
  17. $ages = [42, 51, 45, 48, 19];
  18.  
  19. $sum = 0;
  20.  
  21. for ($i=0; $i<count($ages); $i++)
  22. {
  23. $sum += $ages[$i%5];
  24. }
  25.  
  26. $average = $sum/count($ages);
  27.  
  28. ?>
  29.  
  30. <!DOCTYPE html>
  31. <html>
  32. <head>
  33. <title>Compute average student age</title>
  34. </head>
  35. <body>
  36. <table>
  37. <tr>
  38. <th>Name</th>
  39. <th>Age</th>
  40. </tr>
  41. <?php
  42. foreach ($students as $key=>$value)
  43. {
  44. echo "<tr>\n<td>";
  45. echo $key;
  46. echo "</td>\n<td>";
  47. echo $value;
  48. echo "</td>\n</tr>";
  49. }
  50. ?>
  51. </table>
  52.  
  53. <p>Average age : <?php echo $average; ?></p>
  54.  
  55. </body>
  56. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement