PHP Random Image Generator: Arrays and numbers oh my!
Written by Bean on September 13, 2008 – 5:21 amIn the second week of the PHP 101 for Web Developers course, we introduce arrays through a Random Image Generator. One of things that confuse students most about arrays is how they count. You are probably really used to starting counting with the number 1. When working with arrays, you have to learn how to count from 0!
Arrays store information in a very precise order. At home, you might keep your household bills in a file box organized by month. If you want to know how much your electric bill was last March, you would look in your folder labeled March, the third folder. That assumes you store your bills by month and by year. What about if you stored your monthly bill folders in alphabetical order. It wouldn’t matter as long as you know where to look.
Arrays aren’t as lax about organization as we might be. PHP arrays always assign positions starting with 0. If I created an array of the months, $months = array(‘month1,’month2′,’month3′, ‘month4′, ‘month5′, ‘month6, ‘month7′,’month8,’month9′,’month10′, ‘month11′,’month12′); I would be thinking March is the third month of the year so it is month3. Makes sense to the human so if I said I wanted to pull out the third item in my month array {$months[3]}, I might be pretty confused when I get month4 instead of month3. I get month4 because arrays start counting with 0, so position 3 in an array is really the fourth item in the array. $months = array(‘Jan,’Feb’,'Mar’, ‘Apr’, ‘May’, ‘Jun, ‘Jul’,'Aug,’Sep’,'Oct’, ‘Nov’,'Dec’); To get the value of Mar from the array, I would need to use {$months[2]}.
So what does all of that have to do with our PHP random image generator? In our Goldy example, you were given an array of images that started with goldy0. I added numbers to those file names to organize the images and to show how arrays count positions. The function works regardless if you have numbers on the end of your images or not. I could have used images named goldy32, goldy333, goldy 232 and $images[2]} would return goldy232. If I used goldy, goldfishbowl, goldfish then $images[2]} would return goldfish.
Notice anything missing? There are no file extensions for these image files. There are a lot of random image generators out there but this one has a couple of requirements:
- all the images need to be jpgs
- the jpg file extension is NOT to be included in creating the array
- all of the images need to be stored in the same folder, but you can change the folder path in the script
$images = array(‘fish0′,’fish1′,’fish2′, ‘fish3′, ‘fish4′, ‘fish5′, ‘fish6′);
$i =rand(0, count($images)-1); //do NOT change.
$randomImage = “images/{$images[$i]}.jpg”// change image folder path here if necessary
So in summary:
- it doesn’t matter what you name your images for this php random image generator
- do not include the file extensions in your array
- remember arrays start counting positions at 0
Tags: Arrays, Images, PHP, Random Image Generator
Posted in PHP | 1 Comment »

Bean,
This makes me want to take your course in PHP. I could understand this post. I flunked PHP twice before.
Carol