PHP How to Use Random
PHP How to Use Random:

Again we make use of random values. In this example we use the mt_rand().
You can try the working example to the right.
Be sure also to see the password generator as it uses another random command.

 Random (mt_rand())

How do you Use Random in PHP?

This small example can be used for example in your header, showing a random banner every time a new page is loaded. Or a piece of graphic anywhere on your page.
Here I make use of five graphics. You should rename you graphics 'banner1.png', 'banner2.png', ... etc.

As you see the number/name of the banner is 1-5 and is simply inserted between 'banner' and '.png'

This is a small stand-in for a slideshow, that may be what your are really needing, if you need effects or the images to come in a specific order. Notice that because this is randomly, you may see the same graphic come twice, nothing in any order, and it may not even show image number 3 the first 10 times..
But simple.


    <IMG SRC='graphics/banner<?php echo(mt_rand(1,5)); ?>.png' />
Icons made by Freepik from This is a very simple snip that you can use for showing a random image on every page load. Try reload the page here.