How can CSS classes be effectively used in conjunction with PHP to dynamically change background images on a website?

To dynamically change background images on a website using CSS classes and PHP, you can create a PHP function that generates a random background image URL and then apply this dynamically generated class to the HTML element. This way, each time the page loads, a different background image will be displayed.

<?php
function getRandomBackgroundImage() {
    $images = array('image1.jpg', 'image2.jpg', 'image3.jpg'); // Add more image URLs as needed
    $randomImage = $images[array_rand($images)];
    return $randomImage;
}
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        .random-background {
            background-image: url('<?php echo getRandomBackgroundImage(); ?>');
            background-size: cover;
            background-position: center;
        }
    </style>
</head>
<body class="random-background">
    <!-- Your website content here -->
</body>
</html>