How can CSS be utilized for formatting instead of using outdated attributes like BGCOLOR in PHP-generated tables?

Using CSS for formatting instead of outdated attributes like BGCOLOR in PHP-generated tables involves defining styles in a separate CSS file and applying those styles to the table elements using class or ID attributes. This allows for better separation of content and presentation, making the code cleaner and easier to maintain.

<!DOCTYPE html>
<html>
<head>
    <title>PHP-generated Table with CSS Formatting</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>

<?php
echo "<table>";
echo "<tr><th>Header 1</th><th>Header 2</th></tr>";
for ($i = 1; $i <= 5; $i++) {
    echo "<tr><td>Data $i-1</td><td>Data $i-2</td></tr>";
}
echo "</table>";
?>

</body>
</html>