How can the modulus operator be used to determine if a row should be colored differently in PHP?

To determine if a row should be colored differently in PHP based on its position, we can use the modulus operator %. By using the modulus operator with a specific number (e.g., 2 for every other row), we can check if the row number is divisible by that number. If the row number is divisible, we can apply a different color to that row.

<table>
<?php
for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 == 0) {
        echo '<tr style="background-color: #f2f2f2;">';
    } else {
        echo '<tr>';
    }
    echo '<td>Row ' . $i . '</td>';
    echo '</tr>';
}
?>
</table>