Don't let the sound of your own wheels drive you crazy.”


PHP Random Quote Script

I dig quotes. So, I love to display them randomly for my consistent yet ever-fresh enjoyment.

The past several years I have used this tried and true, easy to implement php script to display random quotes (and images and all kinds of other random information) on websites.

Random Quotes in 3 Easy Steps”

Create an array of quotes

$quotes[] = 'Go ahead, bite the big apple, don\'t mind the maggots, uh huh.';
$quotes[] = 'Happiness is a warm gun. (bang-bang, shoot-shoot.)';
$quotes[] = 'You may be a lover but you ain\'t no dancer.';
$quotes[] = 'Yeah you got lucky, babe. When I found you.';

Generate a random number.

$random_number = rand(0,count($quotes)-1);	

I am first creating a variable called random_number. Then using the PHP rand() function to generate a random integer. You can provide no arguments or a minimum and maximum number. rand(int $min, int $max). That is what we are doing.

What we are using as the minimum number is 0. Remember, the first index in an array is not 1, it is 0.

The number we are giving rand as the maximum is the count of the quotes array - 1. I subtract the 1 because of the fact that the array starts with 0. If we didn't subtract 1 then we could generate a number that is 1 higher then the highest index number in the array.

The final step is to echo the value in the array that has the index of the random number we generated.

echo $quotes[$random_number];	

The whole script could look like this:

<?php
$quotes[] = 'Go ahead, bite the big apple, don\'t mind the maggots, uh huh.';
$quotes[] = 'Happiness is a warm gun. (bang-bang, shoot-shoot.)';
$quotes[] = 'You may be a lover but you ain\'t no dancer.';
$quotes[] = 'Yeah you got lucky, babe. When I found you.';
$quotes[] = 'Out here in the fields, I fight for my meals.';
$quotes[] = 'You go back, jack, do it again. Wheel turnin\' round and round.';
$quotes[] = 'Don\'t let the sound of your own wheels drive you crazy.';
$quotes[] = 'Lord I\'m learning so much more, than back when I knew it all.';
$quotes[] = 'You get too much you get too high. Not enough and you\'re gonna die.';
$quotes[] = 'You were wrong when you said, \'everything was gonna be alright.\'';
$quotes[] = 'I know just what you need. I might just have the thing.';
$quotes[] = 'I am hot, and when I\'m not, I\'m cold as ice.';
$random_number = rand(0,count($quotes)-1);

echo $quotes[$random_number];
?>	



darrenfauth.com is built on the Codeigniter MVC framework and using the Blueprint CSS framework for layout.