What is a more efficient way to determine when to insert a line break in a table row after every second image in a dynamic table built with a while loop in PHP?

One efficient way to determine when to insert a line break in a table row after every second image in a dynamic table built with a while loop in PHP is to use a counter variable to track the number of images displayed. Increment the counter within the loop and check if it is divisible by 2 to determine when to insert the line break.

<?php
// Assuming $images is an array of image URLs
$counter = 0;

echo '<table>';
while ($row = fetch_row_from_database()) {
    echo '<tr>';
    foreach ($row as $image) {
        echo '<td><img src="' . $image . '"></td>';
        $counter++;
        
        if ($counter % 2 == 0) {
            echo '</tr><tr>';
        }
    }
    echo '</tr>';
}
echo '</table>';
?>