3 Ways to Display Random Quotes with PHP
This tutorial will show you 3 different ways to display your favorite random quotes using the dynamic power of PHP. Each method ranks in increasing difficultly, starting out with the most basic PHP function leading up to a more advanced understanding of PHP.
You probably saw the quotes when you arrived here, and now I will show you how it’s done. Let’s get started!
Method #1: A Simple Rand Function
Using the function rand() and switch(), we put together a switch statement then call each quote randomly from the list.
This is the most basic way to show random quotes, but may limit your future conceptualization of PHP if you rely on the most simplistic techniques.
Here’s the code:
<?
//Chooses a random number
$number = Rand (1,3);
//Based on the random number, gives a quote
switch ($number)
{
case 1:
echo "Time is money";
break;
case 2:
echo "Love Thy Neighbor";
break;
case 3:
echo "A Winner Never Quits";
break;
}
?>
Simply displaying the quotes without entering data is the simplest way, but negates the true power of PHP. The next two following examples exemplify many of PHP’s vast capabilities by using arrays to store data.
Method #2: Use Preg-Split to Call Line from Remote File
The second example is a bit more complex because we deal with the preg-split function to pull data from a separate file hosted on our server. Simply load your random quotes in a text file (which forms an array), then randomly call any quote within your designated folder. If you have difficulties uploading files to your server, example 2 is not recommended.
Here’s the code:
<?
srand((double)microtime()*1000000);
$arry_txt=preg_split("/–NEXT–/",join(”,file("quotes.txt")));
echo$arry_txt[rand(0,sizeof($arry_txt)-1)];
?>
After creating the code, create a text file called “quotes.txt” and separate each quote with “–NEXT–” to maintain consistency.
Example
Knowledge Is Power
–NEXT–
I Will Fight No More Forever
I dislike this method the most because it requires too much secondary work in my opinion. Managing the code is enough work; a secondary file increases the chances of error, especially if the text file gets corrupted, deleted from your server, or lost.
Method #3: Pulling Data from Simple Array - My Favorite!
I prefer using arrays when storing data because arrays store code in such a way that can be used in the future without rewriting everything twice. Method 3 creates an array and stores all the random quotes in that array. Then Srand seeds the function rand to make sure number sequences avoid duplication.
Finally, we create a variable called $randomquote, then reuse the rand function to call a random quote from the array.
Here’s the code for my preferred PHP random quote processing method:
<?php
/**
* Add this line of code in your page:
* <?php include "random_quote.php"; ?>
*/
$quotes[] = "Life is Good";
$quotes[] = "Times Equals Money";
srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);
echo "<p>" . $quotes[$randomquote] . "</p>";
?>
Well, there you have it! We discovered 3 different ways to generate random quotes on your website with PHP.
What PHP script do you use to show random quotes? Is there a simplier way to do the above?
Related Articles
- Increase Website Traffic with Google Finance
- Future Plans for This Site
- Aweber Promo Codes | Aweber Promotion Codes