What are some best practices for efficiently implementing random background images on a website using PHP?

When implementing random background images on a website using PHP, it is important to efficiently select and display images to avoid slowing down the site's loading time. One way to achieve this is by predefining a list of image URLs and randomly selecting one to display as the background.

<?php
// Define an array of background image URLs
$backgroundImages = [
    'image1.jpg',
    'image2.jpg',
    'image3.jpg'
];

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

// Output the selected image URL as a background image in CSS
echo '<style>
    body {
        background-image: url("' . $randomImage . '");
        background-size: cover;
        background-repeat: no-repeat;
        background-attachment: fixed;
    }
</style>';
?>