People often ask me how I put those random quotations in the banner and title bar of migraineheartache.

I say to you now that it’s simple. The first thing that you need to know is whether or not your web host supports PHP. If they do, then read on my friend. Otherwise, you must ask them to enable this feature before you may follow the rest of this tutorial.

The other thing I’m going to reccomend before we begin is that you save all of your site’s webpage files with the extension “*.php” rather than “*.htm” or “*.html” Doing so will make this process as well as future ones go much smoother. Ok here we go.

The script itself is very simple. I will show the entire script first then I’ll explain it bit by bit. Here’s the whole thing:

<?php

$quotes[] = "Quote 1";
$quotes[] = "Quote 2";
$quotes[] = "Quote 3";
$quotes[] = "Quote 4";
$quotes[] = "Quote 5";

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

echo "<p>" . $quotes[$randomquote] . "</p>";

?>

Ok, now if you’ve never seen a PHP script or if you’ve never programmed anything before that may appear a little daunting. Let me tell you, its not. I promise. Let’s go through it line by line a bit:

<?php

$quotes[] = "Quote 1";
$quotes[] = "Quote 2";
$quotes[] = "Quote 3";
$quotes[] = "Quote 4";
$quotes[] = "Quote 5";

Ok this part is simple, number one all PHP scripts begin with the “<?php” bit
the rest is a bit more complicated. I’ll do my best to try and convey the concepts, and if you don’t understand, then just copy and paste and skip to the end :D. In PHP and every other programming language, you use “variables” to store data (be it numbers, values, text w/e), variables are named using a string of words, letters or numbers that you can identify with (eg. “$age = 18″). In PHP variables are always defined by putting a ‘$’ before the variable name. Now here we have a variable called “$quotes”, However this one is special. (And I’m really giving you a crash course here but you’ll get it more once we move on) The variable quotes is what we call an array. An array is a type of variable that is really a bunch of values stored in one. An array has spots in it. Think of them like a big long line of buckets, each bucket having a certain item or amount of something in it. The entire line is called “$quotes” but we can reference each individual bucket by saying “$quote[1]” or “$quote[31]” This concept may seem confusing and trivial at first but trust me, its a key part of any programming language. Moving on. The code above shows the line “$quote[] = “Quote 1″;” What each one of these lines essentially does is add another “bucket” to the array called “$quotes” each bucket containing the respective text string. Now lets move on, If you are still confused I encourage you to do a google search for “PHP Array Tutorials” and you shall be further enlightened. Here’s our next bit of code.

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

This bit is the part of the script that is generating the randomness. We first need to tell the computer that we are about to be generating a random number. We do this by giving it a “seed” (Again, google if you are lost) but the command “srand” tells PHP to use the number in the ()’s after “srand” to generate the random number we want. To make it even more random we use an equation rather than a number inside the ()’s. This equation is based off the current time, therefore making sure the number is always truly random. Next we create a new variable called “$randomquote” and we store inside this new variable a number. The command “rand” will return us a number. The syntax for it is “rand(a number, a number);” the two numbers are the range we want to generate in, (eg. pick a number from 1 to 10). The script then calculates how many spaces or “buckets” are in the array “$quotes” and makes sure not to go any higher than that number.

echo "<p>" . $quotes[$randomquote] . "</p>";

?>

this little bit is the fun part. Here we use another new command “echo” this merely prints whatever is after it to the screen, or in this case the user’s web browser. We use a little bit of html combined with the result of the random number we just got. by saying “$quotes[$randomquote]” this tells the computer to look in “$quotes” at the spot “$randomquote”, (”$randomquote” being a number from 0 to however big “$quotes” is). Ok and that’s it for the actual script. Its also important to note that you must close the script with one of these: “?>” Now, Go ahead and edit the file replacing “Quote 1″, “Quote 2″ and such with your own witty comments (Note: You can add as many lines as you want simply copy and paste the “$quote[] = “Blah”;” part over and over) Now save the file as “randomquote.php” and upload to your site in the same directory as whatever page your random quotes are going to be on. Now add this line wherever you would like the random quotes to appear:

<?php include "randomquote.php"; ?>

This tells the webserver to go out the the script “randomquote.php” and run it, and replace that line with whatever comes out of it. (Note: After you’re done, If you view your page and try to view its source code, you won’t see this line of code, you will see the randomly generated quote, this is because the script has already been processed) Ok, well go ahead and try it. If it worked Congratualtions. If not, here are some possible reasons why: Your page might not be saved with the extension “*.php”, or your webserver may not support PHP, or you may have mistyped a symbol somewhere, trust me this happens more than you think.

If you like this script, and would like to learn more about PHP, I reccomend this book: Teach Yourself PHP in 24 Hours 3rd Edition

Its a great read and will surely answer any questions you may have about PHP or programming or any topics discussed here. Ok well that about does it for this tutorial. Wow, we sure covered alot here. I know it went kinda fast, But if you would like more information or help on getting it to work on your site, or if you’d like me to point you to some good sites for getting started in this sort of thing, as usual hit me up at brasonmyguitar@gmail.com, peace out-

-b

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
  • Furl
  • NewsVine
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • De.lirio.us
  • Fark
  • feedmelinks
  • LinkaGoGo
  • Ma.gnolia
  • Netvouz
  • RawSugar
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

One Comment to “Random Quotes PHP Script (Web Design Tips)”  

  1. 1 bailey button ugg

    1898620 drinks about the structure.

Leave a Reply