What are best practices for alternating row colors in table output in PHP?

When displaying tabular data in PHP, it is common practice to alternate the row colors to improve readability and make it easier for users to distinguish between rows. One way to achieve this is by using a simple conditional statement to check if the current row is even or odd, and then applying different CSS classes to style the rows accordingly.

<table>
<?php
$data = array(
    array("John Doe", "johndoe@example.com"),
    array("Jane Smith", "janesmith@example.com"),
    array("Mike Johnson", "mikejohnson@example.com")
);

$counter = 0;
foreach ($data as $row) {
    $class = ($counter % 2 == 0) ? 'even' : 'odd';
    echo '<tr class="' . $class . '">';
    foreach ($row as $cell) {
        echo '<td>' . $cell . '</td>';
    }
    echo '</tr>';
    $counter++;
}
?>
</table>

<style>
.even {
    background-color: #f2f2f2;
}
.odd {
    background-color: #ffffff;
}
</style>