What are the potential pitfalls of using PHP to generate random images on a webpage?

One potential pitfall of using PHP to generate random images on a webpage is that the images may not be truly random if the same image can be selected multiple times in a row. To solve this issue, you can keep track of the previously selected image and ensure that it is not selected again until all other images have been shown.

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

do {
    $randomImage = $images[array_rand($images)];
} while ($randomImage == $prevImage);

$prevImage = $randomImage;

echo '<img src="' . $randomImage . '" alt="Random Image">';
?>