Random Banner Script using PHP

The contents of this article may be out of date. It has been archived and will no longer be updated, comments are closed and the page is provided for reference purposes only.

I wanted to start alternating the banner advertisements on this site so started looking around for the simplest way to randomly display a different banner in my case a few lines of code that included the link, image and some text. I found a number of scripts that all seemed a bit clunky. I started thinking there must be a better simpler way.

All the solutions I found used the php function rand. We end up having to generate a random number from a range e.g. rand (0,100); will give us a random number between 1 and 100. There are a few problems here, firstly the range needs to be set to reflect the number of banners we have in our array so therefore we end up having to count the items in the array or use a fixed number.

The problem with rand is it deals with integers not blocks of code. I started looking for an alternative and stumbled upon shuffle. Shuffle as you would expect takes our array and reorders it at random as you do with a deck of cards. If you shuffle a deck of cards hen take the top card you end up with a random card from the pack each time. The other good thing about shuffle is it shuffles the whole array without us having to specify the number of entries.

So here is the code:

<?php
$advert = array();
  $advert[] = '<a href="#">Banner 1</a>';
  $advert[] = '<a href="#">Banner 2</a>';
  $advert[] = '<a href="#">Banner 3</a>';
shuffle($advert);
echo $advert[0];
?>

Now lets have a look at what we have done line by line.

$advert = array(); Begin a new array named “advert”

$advert[] = '<a href="#">Banner 1</a>'; Create our first advert. Put the HTML code of the advert within an $advert[] variable. The great thing is we can add as many adverts as we want as it is an array and therefore will not overwrite the variable. The example shows three but you can have as many as you like.

shuffle($advert); The PHP function, shuffle, that will randomise our advertisements.

echo $advert[0]; We echo the first advert in the array. The array has been shuffled so the first advert will change.

To use this script your page will need to be able to parse PHP. Usually this means your page will have a .php extension so the server knows it contains code that should be executed before the page is delivered to the users browser.

This article was posted on 27 August 2010 in Code, PHP, Tutorials

comments

What you have had to say about all this...

OK, so I thought the other one was what I was looking for than I stumbled into this related article.  Awesome my friend.  I’m very thankful to have found your site.  This is a very helpful tutorial!

A friendly suggestion for your site: You should include the year in your articles rather than just month and day.  I’d love to know how current some of your articles are.

- Bryce Wisekal

Excellent, just what I was looking for. I almost downloaded a bloated Wordpress plugin for this, but instead just whacked the code in a PHP enabled text widget. Easy.

- Jon Wade

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.