How can a counting variable be used to determine which color to display in a table in PHP?

To determine which color to display in a table using a counting variable in PHP, you can use the modulus operator (%) to cycle through an array of colors based on the value of the counting variable. This way, each row in the table will be assigned a different color based on the counting variable.

<?php
$colors = array('red', 'blue', 'green', 'yellow');
$count = 0;

// Loop through your table rows
foreach ($rows as $row) {
    $color = $colors[$count % count($colors)];
    echo '<tr style="background-color: ' . $color . '">';
    
    // Output your table data here
    
    echo '</tr>';
    
    $count++;
}
?>