(This article is about building your own rotator. You may also be interested in my list of random quotes.)

Yeah, remember when I threatened to post the code for my rotator? Well, uh… there's been an overwhelming demand for this code… Ok, shut up. I'm posting it because I want to. The whole thing is a lot more simple than you might think.
 

JavaScript rotator

First we create the script tag:

<script type="text/javascript">

</script>

 

Next we add a few quotes. To do this, we create an array:

<script type="text/javascript">
var myRandomQuotes=new Array(

);
</script>

 

It's not much of an array without any data, so we add that next:

<script type="text/javascript">
var myRandomQuotes=new Array(
'You take the blue pill, you wake up in your bed and believe whatever you want to believe.',
'You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.'
);
</script>

 

Note the structure of those two array items. Each item in the array begins and ends with a single quote. Array items are separated by a comma. The last item in the array does not end with a comma. In this example, array items are also separated by a line break, but that's not required.

Before moving on to the next step, let's take a moment to talk about quotation marks. Each array item begins and ends with a single quote, so single quotes and apostrophes in the array items will cause problems. To prevent our punctuation from being misinterpreted, abruptly terminating an array item and causing our script to fail, we need to use the "escape character," like so:

<script type="text/javascript">
var myRandomQuotes=new Array(
'You take the blue pill, you wake up in your bed and believe whatever you want to believe.',
'You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.',
'You\'re faster than this. Don\'t think you are, know you are.',
'If real is what you can feel, smell, taste and see, then "real" is simply electrical signals interpreted by your brain.'
);
</script>

 

To prevent the apostrophes in "You're" and "Don't" from screwing up everything, we add in a backslash to tell the JavaScript interpreter to treat the next character as text rather than a command. You'll see we did not add backslashes for the double quotes around "real." Because our array uses single quotes to define items, the double quotes are ignored.

Four items in our array is enough to get started. Short, simple scripts are easy to troubleshoot, so we'll leave the array alone for now. Next we need to make the array do something:

<script type="text/javascript">
var myRandomQuotes=new Array(
'You take the blue pill, you wake up in your bed and believe whatever you want to believe.',
'You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.',
'You\'re faster than this. Don\'t think you are, know you are.',
'If real is what you can feel, smell, taste and see, then "real" is simply electrical signals interpreted by your brain.'
);
document.write( myRandomQuotes[  Math.round(Math.random()*(myRandomQuotes.length-1)) ] );
</script>

 

I know, I know… fuck-a-doodle-doo that line looks complicated. Explaining exactly how the engine works is a little beyond the purview of this post, so I'll just say it displays a random item from the array and leave it at that.

The meat and potatoes of the script is finished. Now you'll need to decide where to put it and how you want it to display. In my case, I removed the standard blog description tag and put the script in it's place. You can do the same, or drop it into your sidebar or any other place you like. As is, the script generates no HTML tags, so you'll probably want to wrap the script in whatever HTML is appropriate to the place you've chosen.

There are other ways to implement this script, like defining the array in the <head> or making the entire script external, but this is the simplest method.
 

PHP rotator

(You should also at least read the JavaScript version instructions.)

Begin by creating the script tag:

<?php

?>

 

Next we define our array:

<?php
$myRandomQuotes=array(

);
?>

 

Next we fill the array with data:

<?php
$myRandomQuotes=array(
'You take the blue pill, you wake up in your bed and believe whatever you want to believe.',
'You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.',
'You\'re faster than this. Don\'t think you are, know you are.',
'If real is what you can feel, smell, taste and see, then "real" is simply electrical signals interpreted by your brain.'
);
?>

 

Are you picking up on the similarities? The PHP syntax is very similar to JavaScript. The array items are defined in exactly the same way: singles quotes, escape characters for single quote-like punctuation, optional line breaks, comma separated.

Next we add the engine, the part that makes our script do something.

<?php
$myRandomQuotes=array(
'You take the blue pill, you wake up in your bed and believe whatever you want to believe.',
'You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.',
'You\'re faster than this. Don\'t think you are, know you are.',
'If real is what you can feel, smell, taste and see, then "real" is simply electrical signals interpreted by your brain.'
);
echo $myRandomQuotes[rand(0,sizeof($myRandomQuotes)-1)];
?>

 

And that's it. The script itself is done. Just pick a place to put it. Just like with the JavaScript version, the script generates no HTML, so you'll need to wrap the script in whatever tags are appropriate for your situation.
 

JS or PHP? Which is right for you?

Anyone can use the JavaScript version. It will run on any blog that allows the use of scripts. It will also run on static, non-blog websites. For any blogging system that does not use PHP, like Blogger, it's the only choice.

If you're using a system like WordPress, there are advantages to the PHP version. The biggest of these is that PHP is server-side and JavaScript is not. The JavaScript version will download everything and a visitor's web browser runs the script to determine what to display. Because of this, your visitor's browser must be able to run JavaScript for anything to happen. Some devices, like mobile phones and other small-screen browsers, don't support JavaScript at all.

PHP works differently. The PHP script runs on the web server, so only the output is sent to the web browser. In addition to saving you a marginal amount of bandwidth, this guarantees that all viewers will see the content properly.

If you can use PHP, there's not really any reason at all to use the JavaScript version.