blog icon

Random Image using PHP

Ok… this is really going to be simple.
You want to display a random image inside, lets say, a header div.

For the sake of simplicity, put all the images you want to display inside a folder and name them something1.jpg (or png, or gif, or whatever), something2.jpg, something3.jpg, etc…

Now you only have to generate a random number using the php rand() function. This function may receive two parameters: a minimum value and a maximum.
Example: rand(1,30) will return a number between 1 and 30 (inclusive).

In this example I have only 5 images inside a “images” folder. They are named “img1.jpg”, “img2.jpg”, etc…

Filed under: Blog, Tutorials