How can PHP be used to count and format tables in groups of three for better layout control?

To count and format tables in groups of three for better layout control in PHP, you can use a counter variable to keep track of the number of table entries. When the counter reaches three, you can start a new row in the table to create a new group. This will help organize the data into visually appealing groups of three.

<?php
$counter = 0;

echo '<table>';
foreach ($data as $item) {
    if ($counter % 3 == 0) {
        echo '<tr>';
    }
    
    echo '<td>' . $item . '</td>';
    
    $counter++;
    
    if ($counter % 3 == 0) {
        echo '</tr>';
    }
}
echo '</table>';
?>