Explain Codes LogoExplain Codes Logo

Php simple foreach loop with HTML

php
foreach-loop
html-table
conditional-formatting
Nikita BarsukovbyNikita Barsukov·Jan 16, 2025
TLDR

Quickly generate an HTML list from an array with PHP. Combine a foreach loop with HTML markup:

<?php $items = ['Apple', 'Banana', 'Cherry']; ?> <ul> <?php foreach ($items as $item): ?> <!-- Each apple a day, keeps the doctor away! --> <li><?= htmlspecialchars($item) ?></li> <?php endforeach; ?> </ul>

This creates a list item (<li>) for each element in $items, using htmlspecialchars to prevent any sneaky security issues.

Dealing with conditionals

Nothing is truly simple. Sometimes you need conditional formatting for your items. For example, if you really like bananas:

<ul> <?php foreach ($items as $item): ?> <!-- The special one --> <li class="<?= $item == 'Banana' ? 'highlight' : '' ?>"> <?= htmlspecialchars($item) ?> </li> <?php endforeach; ?> </ul>

A conditional class has been added to a list item based on the item's value.

The associative array dance

Much like a good dance partner, foreach loops work well with associative arrays allowing access to both the key and value:

<?php $fruits = ['apple' => 'red', 'banana' => 'yellow', 'cherry' => 'dark red']; ?> <ul> <?php foreach ($fruits as $fruit => $color): ?> <!-- It's all about the color of the dance --> <li> <strong><?= ucfirst($fruit) ?>:</strong> <?= $color ?> </li> <?php endforeach; ?> </ul>

Your dance card (<ul>) now describes the type and color of each fruit dancing at the ball.

From simply list to HTML table

Let's dress our foreach for a spiffy occasion. When presenting database results, for instance, a foreach dressed in a HTML table can impress:

<?php // Wearing the latest fashion in $users array from a database ?> <table> <tr> <th>Name</th> <th>Email</th> </tr> <?php foreach ($users as $user): ?> <tr> <!-- Be ready paparazzis --> <td><?= htmlspecialchars($user->name) ?></td> <td><?= htmlspecialchars($user->email) ?></td> </tr> <?php endforeach; ?> </table>

Dressed up, our loop walks down the aisle, each row shining with user data.

Dynamic dropdown menus creation

The practicality of foreach comes to the fore in dynamically populating dropdown menus:

<select name="fruit"> <?php foreach ($fruits as $fruit => $color): ?> <!-- Let's play catch and throw with options --> <option value="<?= $fruit ?>"> <?= ucfirst($fruit) ?> </option> <?php endforeach; ?> </select>

This select element juggles options for each fruit, with the fruit name as display text.

Guarding against exceptions

Life isn't always a bowl of cherries. Facing empty arrays or invalid iterators outright can help prevent a white screen of death:

<?php if (!empty($items)): ?> <ul> <?php foreach ($items as $item): ?> <!-- Accepting reality as it is --> <li><?= htmlspecialchars($item) ?></li> <?php endforeach; ?> </ul> <?php else: ?> <!-- An echo of empty value, silence in the abyss --> <p>No items found.</p> <?php endif; ?>

A quick check to ensure the array isn't empty prevents run-time errors and keeps Mr. Death Screen away.