How can PHP be used to create tables with alternating row colors?

To create tables with alternating row colors in PHP, you can use a simple conditional statement within a loop that generates the table rows. By checking if the row index is even or odd, you can apply different CSS classes to style the rows with different background colors.

<table>
<?php
$colors = array('light', 'dark'); // Define the alternating row colors
$rows = 10; // Number of rows in the table

for ($i = 0; $i < $rows; $i++) {
    $color = $colors[$i % 2]; // Get the color based on the row index

    echo '<tr class="' . $color . '">';
    echo '<td>Row ' . ($i + 1) . '</td>';
    echo '</tr>';
}
?>
</table>

<style>
.light {
    background-color: #f9f9f9;
}

.dark {
    background-color: #e9e9e9;
}
</style>