Purpose: Choose an image randomly from a specified set and to display it.
A sample of a very simple php class that encapsulates php code. Illustrates common design practices.
<?php class Calvinizer { var $path; var $images; function Calvinizer($path = "images/") { $this->path = $path; $this->InitializeImages(); } function InitializeImages() { $this->images = array(); $this->images[1] = "calvin1.jpg"; $this->images[2] = "calvin2.jpg"; $this->images[3] = "calvin3.jpg"; $this->images[4] = "calvin4.jpg"; $this->images[5] = "calvin5.jpg"; $this->images[6] = "calvin6.jpg"; $this->images[7] = "calvin7.jpg"; $this->images[8] = "calvin8.jpg"; $this->images[9] = "calvin9.jpg"; $this->images[10] = "calvin10.jpg"; $this->images[11] = "calvin11.jpg"; $this->images[12] = "calvin12.jpg"; $this->images[13] = "calvin13.gif"; } function GetImage($index = 0) { if(!array_key_exists($index,$a)) { return; } srand((double)microtime()*1000000); echo '<a href="' . $path . $this->images[$index] . '">'; echo '<img class="float-left" src="' . $this->path . $this->images[$index] . '" />'; echo '</a>'; } function GetRandomImage() { srand((double)microtime()*1000000); echo '<a href="' . $_SERVER['PHP_SELF'] . '" title="click for another Calvin">'; echo '<img alt="One of the many faces of Calvin" class="float-left" src="' . $this->path . $this->images[rand(1,count($this->images))] . '" />'; echo '</a>'; } } ?>