How can PHP be used to display tables with colored cell backgrounds?

To display tables with colored cell backgrounds in PHP, you can use HTML table tags along with PHP code to dynamically set the background color of each cell. You can achieve this by using a loop to iterate over the data and apply different background colors based on certain conditions or values.

<?php
// Sample data for the table
$data = [
    ['Name', 'Age', 'City'],
    ['John', 25, 'New York'],
    ['Alice', 30, 'Los Angeles'],
    ['Bob', 22, 'Chicago']
];

// Define an array of background colors
$colors = ['#ffcccc', '#ccffcc', '#ccccff'];

echo '<table border="1">';
foreach ($data as $row) {
    echo '<tr>';
    foreach ($row as $key => $value) {
        // Set background color based on the index of the row
        $color_index = ($key % count($colors));
        echo '<td style="background-color: ' . $colors[$color_index] . ';">' . $value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>