How can PHP be used to rotate images from a folder and quotes from a text file on a website?
To rotate images from a folder and display quotes from a text file on a website using PHP, you can create an array of image file paths and an array of quotes from the text file. Then, randomly select an image and a quote to display each time the page is loaded.
<?php
// Array of image file paths
$images = glob('images/*.jpg');
// Array of quotes from a text file
$quotes = file('quotes.txt');
// Randomly select an image and a quote
$image = $images[array_rand($images)];
$quote = $quotes[array_rand($quotes)];
// Display the image and quote on the website
echo '<img src="' . $image . '" alt="Random Image">';
echo '<blockquote>' . $quote . '</blockquote>';
?>
Keywords
Related Questions
- What common mistake is made in the provided PHP code snippet that causes the loop to be ignored?
- What are the potential drawbacks or limitations of relying on PHP's date() function for displaying localized month names?
- What are common pitfalls when implementing pagination in PHP, specifically when using MySQL queries?