How can PHP be used to display different images based on the current URL?

To display different images based on the current URL in PHP, you can use a combination of conditional statements and HTML image tags. By checking the current URL using $_SERVER['REQUEST_URI'], you can determine which image to display and dynamically include it in your HTML output.

<?php
$current_url = $_SERVER['REQUEST_URI'];

if ($current_url == '/page1') {
    $image_path = 'image1.jpg';
} elseif ($current_url == '/page2') {
    $image_path = 'image2.jpg';
} else {
    $image_path = 'default.jpg';
}

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