What are some best practices for displaying random images in PHP?

When displaying random images in PHP, it is important to ensure that the images are randomly selected each time the page is loaded to provide variety and avoid predictability. One way to achieve this is by storing the image filenames in an array and using the array_rand() function to select a random index to display.

<?php
// Array of image filenames
$images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'];

// Select a random image from the array
$randomImage = $images[array_rand($images)];

// Display the random image
echo '<img src="' . $randomImage . '" alt="Random Image">';
?>