Creating a random quotes script with PHP
In this tutorial, you will learn how to create a random quotes script, step by step.
You should already know: Basic PHP
So, lets begin!
Here's our first part of code:
<html>
<head>
<title>A Random Quote</title>
</head>
<body>
This starts the HTML page, defines the title of the page (A Random Quote), and begins the body.
Lets look at our next piece of code:
<?php
As you probably already know, this begins our PHP script.
And the next group of code we have is this:
// Declare all of the quotes we will use in an array
$quotes[] = '"I always tried to turn every disaster into an opportunity. ~ John D. Rockefeller"';
$quotes[] = '"It\'s been reported that Britney Spears is having financial trouble and recently took out a $5 million loan. Kevin Federline offered to co-sign the loan and then everyone had a big laugh."';
$quotes[] = '
"Yesterday, Baltimore slugger Rafael Palmiero tested the positive for steroids a few months after telling congress, \'I never took steroids, period.\' Then today, Palmiero said, \'I mean to say \'I never took steroids, question mark.\'"';
$quotes[] = '"This week, rapper Snoop Dogg started doing a series of commercials for Chrysler automobiles. This marks the first time Snoop has recommended a dealer who sells cars."';
$quotes[] = '
"Britney Spears is reportedly fighting with her husband Kevin Federline because he\'s insisting that their child be named after him. Apparently Britney told him, \'Why would I want to name him \'Lazy Jackass?\'"';
This is the heart of our random quotes script. It's where we define all of our quotes in something called an array. So, we say $quotes[] = 'Some quote'
What that means is, we add on to the array, which is named "quotes". Each quote actually has a number. So, at any time, we can do something like: $quotes[3] Which would actually call the second quote in our list. When we say: $quotes[] = 'something', we don't put a number in the []. You can, but PHP autonumbers it, so it automaticlay assigns the next number to that quote.
Another important thing to note is that the array numbering starts at 0. So, the first quote would be acessed like this: $quotes[0].
Now, lets move on to the next piece of code:
// Get the number of quotes we have, and subtract one because arrays start at 0
$number_quotes = count($quotes) - 1;
The purpose of this line is simple. We create a variable, and then assign the value of the upper range to it. So, if we have 10 quotes, the value of the variable becomes 9. Here's how that happens. we use the count() function, which counts all of the elements in the array $quotes. So, if we had 10 quotes, that count($quotes) would come up with 10. Then, we subtract 1 from that 10. We do that because the array starts at 0, so we don't have an array element 10.
Lets go on to the next piece of code:
// Pick a quote number randomly between 0, the beggining of the array, and $number_quotes which stands for the number of qoutes we have minus 1
$quote_number = rand(0, $number_quotes);
This is where we actually select a number randomly to use to get the random quote. So, we use the rand() function. First we have rand(0 That says the minimum our random number can be is 0. Then, we have a $number_quotes, which says our maximum random number can be the value of $number_quotes, the number of quotes we have minus 1.
As you can see, we give the variable $quote_number the value of the random number we just selected.
Lets look at our last piece of code:
// Ouotput the quote that matches the random number we came up with
echo $quotes[$quote_number];
On that line, we access one of the quotes in our quote array by using $quotes[$quote_number]. Lets say $quote_number (our random number) had a value of 5. It turns that line into: echo $quotes[5]. So, it takes the quote that has they key of the random number chosen, and outputs it to the screen.
Just in case you missed something, here is the complete random quotes script:
<html>
<head>
<title>A Random Quote</title>
</head>
<body>
<?php
// Declare all of the quotes we will use in an array
$quotes[] = '"I always tried to turn every disaster into an opportunity. ~ John D. Rockefeller"';
$quotes[] = '"It\'s been reported that Britney Spears is having financial trouble and recently took out a $5 million loan. Kevin Federline offered to co-sign the loan and then everyone had a big laugh."';
$quotes[] = '
"Yesterday, Baltimore slugger Rafael Palmiero tested the positive for steroids a few months after telling congress, \'I never took steroids, period.\' Then today, Palmiero said, \'I mean to say \'I never took steroids, question mark.\'"';
$quotes[] = '"This week, rapper Snoop Dogg started doing a series of commercials for Chrysler automobiles. This marks the first time Snoop has recommended a dealer who sells cars."';
$quotes[] = '
"Britney Spears is reportedly fighting with her husband Kevin Federline because he\'s insisting that their child be named after him. Apparently Britney told him, \'Why would I want to name him \'Lazy Jackass?\'"';
// Get the number of quotes we have, and subtract one because arrays start at 0
$number_quotes = count($quotes) - 1;
// Pick a quote number randomly between 0, the beggining of the array, and $number_quotes which stands for the number of qoutes we have minus 1
$quote_number = rand(0, $number_quotes);
// Ouotput the quote that matches the random number we came up with
echo $quotes[$quote_number];
?>
</body>
</html>
If you have any questions, just post them in this thread.