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">';
?>
Keywords
Related Questions
- In what scenarios should preg_quote be used in conjunction with preg_grep in PHP code?
- What are the benefits of only passing necessary data to forms in PHP and querying additional data during processing?
- How can hosting providers like Strato impact the performance and behavior of PHP scripts over time?