Blinding Light: Free Web Layouts, Anime Transparent PNG, PS Brushes, Coding Tutorials and more!

Blinding Light: Web Layouts, PS Brushes, Coding Tutorials, Transparent PNG

Randomizing With PHP

This tutorial teaches you how to display random links, images, quotes, etc. Ready to learn? Let's get started then...

Step 1: What To Randomize?

For the sake of this tutorial, let's randomize links. As an example, we will randomly display the link to either one of these search engines: Google, Yahoo, Altavista and Ask Jeeves.

Now that we know what we want to randomize, it's time to organize them in an array. Here's the first part of the code:

<?php

$random[] = "<a href=\"http://google.com\">Google</a>";
$random[] = "<a href=\"http://yahoo.com\">Yahoo</a>";
$random[] = "<a href=\"http://altavista.com\">Altavista</a>";
$random[] = "<a href=\"http://ask.com\">Ask Jeeves</a>";

Notice the underlined text? Those are the HTML codes for the link to the search engines we want to display at random. Take note that if you're going to place a quotation mark (") on the underlined part, you need to place a slash (\) before it. This is to prevent PHP parse errors. So, if you want the links to open in a new window, you add this in the a attribute of your code:

target=\"_blank\"

Got it? You can change the underlined text to anything you want: codes to image links, quotations from famous people, etc. Just don't forget the slash (\) before a quotation mark (").

You can add more to the array of random things to display. Just remember that if you're adding more items, the format should be like this:

$random[] = "WHATEVER YOU WANT TO RANDOMIZE";

Everything clear so far? This is just the beginning. Let's finish this code off, shall we?

Step 2: Displaying Randomness

Next, we're going to add the final lines of code to the one we discussed earlier:

echo $random[rand(0,count($random)-1)];

?>

That's basically all you need to display random things. For those who would care to know, here is a more detailed explanation of the code. We will start with the innermost brackets:

count($random)-1

Counts how many values there are in the array and subtracts it by 1. This will give you the highest key value in our array.

Huh? Don't quite get it? In our example, we have 4 values (Google, Yahoo, Altavista and Ask Jeeves) and each automatically has a numerical key assigned when we placed them in an array. These keys start with the number 0. Therefore, in the case of our code, Google is assigned key number 0, Yahoo is assigned key number 1, Altavista is assigned key number 2, and Ask Jeeves is assigned key number 3.

random(0,count($random)-1)

Gets a random number from 0 to the highest key value in our array which, based on our code, is 3.

echo $random[rand(0,count($random)-1)];

Displays the value of whatever corresponds to the random number. So, for example, if our random number is 1, it will display the link to Yahoo.

Conclusion

This is what the completed code should look like:

<?php

$random[] = "<a href=\"http://google.com\">Google</a>";
$random[] = "<a href=\"http://yahoo.com\">Yahoo</a>";
$random[] = "<a href=\"http://altavista.com\">Altavista</a>";
$random[] = "<a href=\"http://ask.com\">Ask Jeeves</a>";

echo $random[rand(0,count($random)-1)];

?>

Just a few lines of code and see how simple it is to display random stuff? Try it out for yourself.

If you have any questions or if you want to discuss this tutorial, you may post at Blinding Light MB.

Web site design, scripting and contents © 2003-08 Miko Reznor.
No part of this site may be republished without permission.