How can PHP handle caching issues with images displayed through scripts?

When displaying images through PHP scripts, caching issues can arise where the browser may not update the image even if it has been changed on the server. One way to handle this is by appending a unique query string to the image URL based on its last modification time. This forces the browser to fetch the updated image instead of using the cached version.

<?php
$imagePath = 'path/to/image.jpg';
$imageLastModified = filemtime($imagePath);
$imageUrl = 'http://example.com/image.jpg?' . $imageLastModified;

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