How can the code snippet provided be optimized to avoid the issue of carrying over images from previous loop iterations in PHP?

The issue of carrying over images from previous loop iterations in PHP can be solved by clearing the output buffer before processing each image. This ensures that only the current image is displayed without any remnants from previous iterations.

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

foreach ($images as $image) {
    ob_clean(); // Clear output buffer
    echo "<img src='$image' alt='Image'>";
}
?>