What is the best practice for changing table row colors dynamically in PHP?

When dynamically changing table row colors in PHP, the best practice is to use a conditional statement to alternate between different CSS classes for each row. This can be achieved by using a loop to iterate through the rows of the table and applying a different class based on the row number.

<table>
<?php
$colors = array('odd', 'even'); // Define CSS classes for odd and even rows
$row_count = 0; // Initialize row count

while ($row = $result->fetch_assoc()) {
    $class = $colors[$row_count % 2]; // Alternate between odd and even classes
    echo "<tr class='$class'>";
    // Output table data here
    echo "</tr>";
    $row_count++; // Increment row count
}
?>
</table>