What are the potential pitfalls of using a counter variable in PHP to determine table colors?

Using a counter variable to determine table colors in PHP can lead to inconsistent results if the counter is not properly reset or incremented. To avoid this issue, it is recommended to use CSS classes to style the table rows based on odd/even rows instead of relying on a counter variable.

<table>
    <?php
    $rows = ['Row 1', 'Row 2', 'Row 3', 'Row 4'];
    foreach ($rows as $key => $row) {
        $class = ($key % 2 == 0) ? 'even' : 'odd';
        echo "<tr class='$class'><td>$row</td></tr>";
    }
    ?>
</table>