How can a counter variable be effectively used to control line breaks in PHP image output?

To control line breaks in PHP image output, a counter variable can be used to keep track of the number of images outputted per line. By incrementing the counter variable after each image output, you can check if the maximum number of images per line has been reached and insert a line break accordingly.

<?php
// Define the maximum number of images per line
$max_images_per_line = 3;
// Initialize the counter variable
$counter = 0;

// Loop through your images and output them
foreach ($images as $image) {
    echo '<img src="' . $image . '" alt="Image">';
    
    // Increment the counter
    $counter++;
    
    // Check if maximum images per line reached and insert line break
    if ($counter % $max_images_per_line == 0) {
        echo '<br>';
    }
}
?>