How can one effectively implement a line break after every three images in a loop in PHP?

To implement a line break after every three images in a loop in PHP, you can use a counter variable to keep track of the number of images displayed. Once the counter reaches three, you can output a line break before displaying the next image. This will create a new line after every three images in the loop.

<?php
$images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg', 'image6.jpg'];

$counter = 0;
foreach($images as $image) {
    echo '<img src="' . $image . '" alt="image">';
    $counter++;
    
    if($counter % 3 == 0) {
        echo '<br>';
    }
}
?>