repl.it
@malvoliothegood/

PHP Tutorial: foreach loop

PHP Web Server

No description

fork
loading
Files
  • index.php
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<html>
  <head>
    <title>Favourite Foods and their Ratings</title>
    <style>
      body {
        font-size: 19px;
        font-family: sans-serif;
        line-height: 1.6;
      }
      h1 {
        font-weight: 400;
        font-size: 1.85em;
      }
      table {
        min-width: 200px;
      }
      table, th, td {
        border-collapse: collapse;
        border: 1px solid #ccc;        
      }
      th, td {
        padding: 2px 4px;
      }
    </style>
  </head>
  <body>
    <h1>My Favourite Foods and their Ratings</h1>
    <p>A list is used to display the information. However, there is commented out code that shows how to display it using a table. This is possibly a nicer format.</p>
    <?php

    $fav_foods = ["fries" => 1, "pizza" => 3, "nachos" => 4, "sushi" => 1, "burgers" => 2];  
    
    echo "<ul>";
    foreach ($fav_foods as $food => $rating) {
        echo "<li>" . $food . ",  " . $rating . "</li>";
    }
    echo "</ul>";

    
    /*
     $fav_foods = ["fries" => 1, "pizza" => 3, "nachos" => 4, "sushi" => 1, "burgers" => 2];

    echo "<h1>My Favourite Foods and their Ratings</h1>";
    echo "<table>";
    echo "<theader>";
    echo "<tr>";
    echo "<th>Food</th><th>Rating</th>";
    echo "</tr>";
    echo "</theader>";
    foreach ($fav_foods as $food => $rating) {
        echo "<tr><td>" . $food . "</td><td>" . $rating . "</td></tr>";
    }
    echo "<tbody></table>";
    */    
    
    ?> 
  </body>
</html>
?