I was recently reading David Thorne’s frankly hilarious antics with a Magic 8 Ball. He spent a day responding to every question with a randomly-generated phrase using the famous fortune-telling toy. But why buy a Magic 8 Ball when you can make it at home for free with a little bit of PHP code? People will be flocking to your website for this kind of advice.The Magic 8 Ball offers 20 possible answers; ten are positive, five are ambivalent and the rest are negative. In this tutorial we will use the original (and best) answers.
Making a Magic 8 Ball
To make the PHP Magic 8 Ball, we will use two simple functions:
- The
Randfunction will generate a random number between 1 and 20 - The
switchfunction will output one of 20 possible result according to which number is randomly generated
<?php
//Defines $num as a random number (starting from 1, up to 20)
$num = Rand (1,20);
//Uses the number $num to output that answer
switch ($num)
{
case 1: ?>
<p>It is certain</p>
<?php ; break;
case 2: ?>
<p>It is decidedly so</p>
<?php ; break;
case 3: ?>
<p>Without a doubt</p>
<?php ; break;
case 4: ?>
<p>Yes – definitely</p>
<?php ; break;
case 5: ?>
<p>You may rely on it</p>
<?php ; break;
case 6: ?>
<p>As I see it, yes</p>
<?php ; break;
case 7: ?>
<p>Most likely</p>
<?php ; break;
case 8: ?>
<p>Outlook good</p>
<?php ; break;
case 9: ?>
<p>Signs point to yes</p>
<?php ; break;
case 10: ?>
<p>Yes</p>
<?php ; break;
case 11: ?>
<p>Reply hazy, try again</p>
<?php ; break;
case 12: ?>
<p>Ask again later</p>
<?php ; break;
case 13: ?>
<p>Better not tell you now</p>
<?php ; break;
case 14: ?>
<p>Cannot predict now</p>
<?php ; break;
case 15: ?>
<p>Concentrate and ask again</p>
<?php ; break;
case 16: ?>
<p>Don't count on it</p>
<?php ; break;
case 17: ?>
<p>My reply is no</p>
<?php ; break;
case 18: ?>
<p>My sources say no</p>
<?php ; break;
case 19: ?>
<p>Outlook not so good</p>
<?php ; break;
case 20: ?>
<p>Very doubtful</p>
<?php ; }
?>
Taking it further: More random things
Having rendered the Magic 8 Ball obsolete, we can use the same idea for more (or less) constructive purposes. There are as many random things to display on your website as there are random things. Here are just three more examples, which I hope will encourage you to play with the Rand and switch functions for yourself.
Random image generator
This example displays a random image each time the page is loaded.
<?php
//Defines $num as a random number (starting from 1, up to 3)
//To include more, change 3 to the total number of images
$num = Rand (1,3);
//Uses the number $num to output that image
switch ($num)
{
//Each case must begin with a colon and end with a semicolon
//There must be a break; between each of the cases
case 1: ?>
<img src="image1.jpg" alt="Image 1" />
<?php ; break;
case 2: ?>
<img src="image2.jpg" alt="Image 2" />
<?php ; break;
case 3: ?>
<img src="image3.jpg" alt="Image 3" />
<?php ; }
?>
Random quote generator
This example displays a random verse from Psalm 23 each time the page is loaded.
<?php
//Defines $num as a random number (starting from 1, up to 6)
$num = Rand (1,6);
//Uses the number $num to output that verse of Psalm 23
switch ($num)
{
case 1: ?>
<p>The LORD is my shepherd; I shall not want.</p>
<?php ; break;
case 2: ?>
<p>He maketh me to lie down in green pastures: he leadeth me beside the still waters.</p>
<?php ; break;
case 3: ?>
<p>He restoreth my soul: he leadeth me in the paths of righteousness for his name's sake.</p>
<?php ; break;
case 4: ?>
<p>Yea, though I walk through the valley of the shadow of death, I will fear no evil: for thou art with me; thy rod and thy staff they comfort me.</p>
<?php ; break;
case 5: ?>
<p>Thou preparest a table before me in the presence of mine enemies: thou anointest my head with oil; my cup runneth over.</p>
<?php ; break;
case 6: ?>
<p>Surely goodness and mercy shall follow me all the days of my life: and I will dwell in the house of the LORD for ever.</p>
<?php ; }
?>
Random breeds of dog in PHP
This example displays a random breed of dog (with link to a picture) each time the page is loaded.
<?php
//Defines $num as a random number (starting from 1, up to 3)
$num = Rand (1,3);
//Uses the number $num to output one of these three delightful breeds of dog
//(Also including a link to a pretty photo)
switch ($num)
{
case 1: ?>
<a href="http://en.wikipedia.org/wiki/File:Afghan_Hound.jpg">Afghan Hound</a>
<?php ; break;
case 2: ?>
<a href="http://en.wikipedia.org/wiki/File:American_Eskimo_Dog.jpg">American Eskimo Dog</a>
<?php ; break;
case 3: ?>
<a href="http://en.wikipedia.org/wiki/File:Bichon_Fris%C3%A9_-_studdogbichon.jpg">Bichon Frisé</a>
<?php ; }
?>
Two random things: colours and their names
In this final example of what you can do with the rand and switch functions, we will introduce a second random element. We will select a random colour just as before, but we will also give it a lick of paint in another random colour. Brain scientists use this kind of trick to demonstrate a conflict between how the different areas of your brain process information. If you refresh the page several times while trying to say the colours out loud you will probably find yourself reading the words instead.
<?php
//Defines $num as a random number (starting from 1, up to 5)
$num = Rand (1,5);
//Defines $name as a random number (starting from 1, up to 5)
$name = Rand (1,5);
//Uses the number $num to define $color as one of these five colours
switch ($num)
{
case 1:
$color = 'green'
; break;
case 2:
$color = 'red'
; break;
case 3:
$color = 'blue'
; break;
case 4:
$color = 'black'
; break;
case 5:
$color = 'orange'
; }
?>
<?php
//Based on the second random number, defines the colour of the selected colour name as $color
switch ($name)
{
case 1: ?>
<h1 style="color:<?php echo $color ?>">Green</h1>
<?php ; break;
case 2: ?>
<h1 style="color:<?php echo $color ?>">Red</h1>
<?php ; break;
case 3: ?>
<h1 style="color:<?php echo $color ?>">Blue</h1>
<?php ; break;
case 4: ?>
<h1 style="color:<?php echo $color ?>">Black</h1>
<?php ; break;
case 5: ?>
<h1 style="color:<?php echo $color ?>">Orange</h1>
<?php ; }
?>
What else can you do?
As I said above, there really are as many things to do with these functions as there are random things. These examples provide a good base to build some fantastic random-thing generators. If you cook up something nice and random, drop me a line in the comments and tell me all about it!

