πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English | πŸ‡ͺπŸ‡Έ EspaΓ±ol | πŸ‡΅πŸ‡Ή PortuguΓͺs | πŸ‡ΉπŸ‡­ ΰΉ„ΰΈ—ΰΈ’ | πŸ‡¨πŸ‡³ δΈ­ζ–‡

[PHP for Beginners] How to Dynamically Display a News/Notice List with a foreach Loop

You often see "News" or "Notices" lists on websites, right? It's a hassle to manually update the HTML every time new information is added. Actually, by using PHP's foreach loop, you can automate this tedious task and easily create a dynamic list that always displays the latest information.

In this article, we'll explain the basics and applications of PHP's `foreach` loop using a practical notice list example, so even programming beginners can experience getting it to "just work" by simply copying and pasting. Let's master the flow of taking data stored in an array one by one and displaying it as an HTML list together!

To get your PHP running (development environment), if you haven't set up XAMPP yet, please refer to [PHP for Beginners] A Complete Guide to Downloading and Installing XAMPP!. After installation, place your PHP file in the specified folder (like `htdocs`), and you're all set!

1. The Absolute Basics! PHP Arrays and the foreach Loop

First, to understand what `foreach` does, let's start with its partner, the "array."

An array is like a box that can store multiple values together. For example, if you want to manage notice information as sets of "date" and "title," you can create an array like this:

<?php
// Array storing the news information
$news_items = [
  [
    "date" => "2025-07-03",
    "title" => "Notice of Summer Holiday Closure"
  ],
  [
    "date" => "2025-07-01",
    "title" => "Launched New Service '〇〇'"
  ],
  [
    "date" => "2025-06-20",
    "title" => "Our Website Has Been Renewed"
  ]
];

// Check the contents of the array (for debugging)
print_r($news_items);
?>

The job of the `foreach` loop is to take items out of this "box" called an array one by one and process them. The syntax is very simple:

foreach (array as $variable_for_temporary_element) { ...processing... }

Using this syntax, let's try to take out just the titles from the previous array in order and display them.

<?php
$news_items = [
  ["date" => "2025-07-03", "title" => "Notice of Summer Holiday Closure"],
  ["date" => "2025-07-01", "title" => "Launched New Service '〇〇'"],
  ["date" => "2025-06-20", "title" => "Our Website Has Been Renewed"]
];

// Take out each notice from the array one by one
foreach ($news_items as $item) {
  // Display the title of the notice that was taken out
  echo $item['title'] . "\n";
}
?>

As you can see, `foreach` automatically repeats the process until there are no more elements left in the array. This is the heart of dynamic list generation.

2. In Practice! Creating a Notice List with foreach

Now for the main event. Let's use the array and `foreach` loop from before to create a notice list in HTML format that can be displayed on a web page. The following code is the most basic form combining HTML and PHP. If you copy and paste this into a file, you can view it in your browser.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Notice List</title>
    <style>
        body { font-family: sans-serif; line-height: 1.6; padding: 20px; }
        .news-list { list-style: none; padding: 0; border-top: 1px solid #ccc; }
        .news-list li { padding: 15px 10px; border-bottom: 1px solid #ccc; }
        .news-date { font-weight: bold; margin-right: 1em; color: #555; }
    </style>
</head>
<body>

    <h1>Notices</h1>

    <?php
    // Prepare the notice information as an array
    $notices = [
        [
            "date" => "2025-07-03",
            "title" => "Notice of Summer Holiday Closure"
        ],
        [
            "date" => "2025-07-01",
            "title" => "Launched New Service '〇〇'"
        ],
        [
            "date" => "2025-06-20",
            "title" => "Our Website Has Been Renewed"
        ]
    ];
    ?>

    <ul class="news-list">
        <?php foreach ($notices as $notice): ?>
            <li>
                <span class="news-date"><?php echo htmlspecialchars($notice['date']); ?></span>
                <span class="news-title"><?php echo htmlspecialchars($notice['title']); ?></span>
            </li>
        <?php endforeach; ?>
    </ul>

</body>
</html>

The key point of this code is that the `foreach` loop runs inside the `