PHP Echo Tutorials

Image Rotation

Written by phpecho.com   

In this small tutorial, we'll learn how to rotate images in an array. This can be very handy with websites that have advertising programs. You should have a very basic knowledge of how PHP works, and your server doesn't need anything special - just PHP.

To start off, we need to create an array. In this we will place all of the images we wish to be rotated.

<?php
 $images = array(
 'http://www.host.com/image.gif',
 'http://www.host.com/image2.gif',
 'http://www.host.com/image3.gif',
 'http://www.host.com/image4.gif'
 );

Now that we have that under our belt, let's setup the randomizer. We'll be using the function array_rand().

$image = $images[array_rand($images, 1)];

And finally, let's display the image in a proper image tag, and close off the document.

echo '<img src="' . $image . '" alt="Image" border="0" />';
 ?>

And that's all, folks! Thanks for reading!