Is it possible to use the rand function in PHP to display a new image every 10 seconds?
To display a new image every 10 seconds using the rand function in PHP, you can create an array of image file paths, use the rand function to select a random image from the array, and then use JavaScript to refresh the image every 10 seconds.
<?php
$images = ['image1.jpg', 'image2.jpg', 'image3.jpg']; // Array of image file paths
$random_image = $images[rand(0, count($images) - 1)]; // Select a random image from the array
echo '<img src="' . $random_image . '" id="randomImage">'; // Display the random image
echo '<script>
setInterval(function() {
var images = ' . json_encode($images) . ';
var randomImage = images[Math.floor(Math.random() * images.length)];
document.getElementById("randomImage").src = randomImage;
}, 10000); // Refresh the image every 10 seconds
</script>';
?>
Keywords
Related Questions
- What is the significance of choosing the correct encoding, such as ANSI or UTF-8, when working with PHP scripts?
- What are the necessary tools and software required to start programming in PHP?
- How can developers ensure the security and reliability of email attachments when sending them programmatically in PHP?