How can you alternate the styling of every second row in a table generated by PHP?

To alternate the styling of every second row in a table generated by PHP, you can use a simple conditional statement within a loop that iterates over the rows of the table. By checking if the row index is even or odd, you can apply different CSS classes to style the rows accordingly.

<table>
<?php
$rows = 10; // Number of rows in the table

for ($i = 1; $i <= $rows; $i++) {
    if ($i % 2 == 0) {
        echo '<tr class="even">';
    } else {
        echo '<tr class="odd">';
    }
    
    echo '<td>Row ' . $i . '</td>';
    echo '</tr>';
}
?>
</table>

<style>
    .even {
        background-color: lightgray;
    }
    .odd {
        background-color: white;
    }
</style>