i am dustin diaz

a JavaScriptr...

boosh.

don't worry about it.

A Simple PHP image rotator

This has probably been done a thousand times over by many programmers, but I thought I'd offer just one more to the collection.

Three Simple Steps

PHP: Function randomImage

<?php

function randomImage ( $array ) {

  $total = count($array);

  $call = rand(0,$total-1);

  return $array[$call];

}

?>

The Image Array

<?php

$my_images = array (

  "dustin.jpg",

  "Jeffrey.jpeg",

  "Chrissy.png",

  "Kristen.gif"

);

?>

The embedded PHP into your XHTML:

<?php

echo '<img 

  src="'.randomImage($my_images).'" 

  alt="Random Image" />';

?>
Why did I bother writing this? Because I've seen way too many scripts and downloads and all that yada yada just to make some simple random image rotator. If you want to add more images, just put in an extra item into the array.

Case closed.

this is who i am

Hi, my name is Dustin Diaz and I'm an Engineer @ObviousCorp. Previously @Twitter, @Google, and @Yahoo, author of Strobist® Info co-author of JavaScript Design Patterns, co-creator of the Ender JavaScript Framework, a Photographer, and an amateur Mixologist. This is my website. Welcome!

On this site I write about JavaScript. You can also follow along with my open-source work on Github.

This site is optimized and works best in Microsoft Internet Explorer 6.