How can PHP be used to optimize background image loading and caching on webpages?

Background images on webpages can be optimized for loading and caching by using PHP to dynamically generate the CSS code. This allows for setting cache-control headers and implementing lazy loading techniques to improve performance. By using PHP, we can also generate unique filenames for each background image to prevent browser caching issues.

<?php
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: max-age=31536000");

$bg_image = "path/to/background-image.jpg";
?>
body {
    background-image: url('<?php echo $bg_image; ?>');
}