my first php script – dynamically rotating images

I’m proud of myself yesterday afternoon I wrote my first ever tiny php script. I’ve done lots of editing of php, since all the forum software and CMS’s use php, and I’ve become familiar with the syntax. However at university I learn Java and the entire course was entirely object oriented……so I struggled to get my head around a non object oriented language.

Anyway, I’ve given it a break for a while, but yesterday (with the help of Alexander who is on holidays at the moment) I returned to finishing off the new site I’ve been working on for, and Alexander and I were discussing a dynamic image display (like a rotating banner, but not precisely a “banner” for our project). I’m sure it’s easy enough to find thousands of php scripts online that do just the job, but just as I was about to google for one, I said to Alexander “you know what? I should see if I can try and write one myself.” When I said that I didn’t honestly believe I could get a working banner, but Alex had done a similar little task once while trying to learn php and he’s kept telling me I should try and do the same and learn more php this way.

So, I tried it. Started off with a variable, a random number generator and wrote a simple script first that had some details hard coded. With some suggestions from Alex and not much editing, we got it to work! The idea is, that for each random image will display an image with a custom “alt” tag for each image plus a link to a website.

I ended up with this:


/* first integer is minimum and must not be changed. Second integer is number of images – 1 (ie. the size of the array) */
$random_number = rand (0 , 9);
$width = ‘156’; /*width of the images */

/* Array number must match image number image(n).jpg, ie. image0.jpg */
$image[0] = array (‘’, ‘groupfitness website’);
$image[1] = array (‘#’, ‘1’);
$image[2] = array (‘#’, ‘2’);
$image[3] = array (‘#’, ‘3’);
$image[4] = array (‘#’, ‘4’);
$image[5] = array (‘#’, ‘5’);
$image[6] = array (‘#’, ‘6’);
$image[7] = array (‘#’, ‘7’);
$image[8] = array (‘#’, ‘8’);
$image[9] = array (‘#’, ‘9’);

echo ‘<a href=”‘ . $image[$random_number][0] . ‘” target=”_blank”><img border=”0″ width=”‘ . $width . ‘” src=”images/image’ . $random_number . ‘.jpg” alt=”‘ . $image[$random_number][1] . ‘” /></a>’;


Now I’ve set up an array of different images. The one draw back is that everytime we add an image, we have to change the size of the random number generator and add the url and alt tag info, but it’s a simple script that will serve our purpose, and I’m really proud I wrote it myself! 🙂

I have wondered though about the two different methods I’ve used instantiating a multidimensional array. Personally I think that writing:

$image[0] = array (‘’, ‘groupfitness website’);

should be identical to:

$image[0][0] = ‘’;
$image[0][1] = ‘groupfitness website’;

However I’m not sure if the way I have instantiated the variable means that I have created an array, sitting inside an array, which creates the same output but could create extra overhead. Probably not applicable in such a small script as this one, but it’s just got me wondering.

I don’t know the answer, but I’m inclined to think that the way I have done it accesses the array memory space in the same way as the second example, but it’s difficult to find out the answer for sure! If there’s any php guru’s out there I’d love to know the answer to this one!