IT Support Contracts, Website Design, Telephone Systems, Marine Installation and ISO 27001 Accreditation

How to Display a Random Image on Website Using PHP

Keeping a web site dynamic is always nice from a user experience perspective. It keeps users coming back to check on the site for different content and it keeps the experience fresh with each visit.

What you will need:

  • A web server capable of running PHP

1. The first step is to rename all of the files you are wanting to use for your dynamic image. If this is a banner then the images should also all be the same size so the layout wont be affected. Begin by renaming your images all the same followed by an incrementing number e.g banner1.jpg, banner2.jpg, banner3.jpg

2. The next step is to add the code. Find the place where you want the image to be displayed and add the following snippet of code: <img src="banner<?php echo rand(1, 10); ?>.jpg" />

3. All you need to do then is change the number 10 to however many images you have that you wish to scroll through. now everytime someone comes to your page it will display a random image from the set you have.