Random Content With PHP
Sometimes it is desirable to display randomly selected content using PHP. This article shows you how.
Like other programming languages, the PHP random number function is pseudo-random rather than true random. Over time, it might or might not display each selection a similar number of times. It is possible the same item will be selected consecutively.
I'll show you two ways to insert randomly selected content into a PHP web page.
-
Imported — Content imported from a randomly selected file.
The imported file can be an image, flash content, an advertisement, a poem, a saying — anything that can be published on a web page. The imported file can be of any length reasonable for a web page.
-
Embedded — Random content obtained from within the PHP code itself.
This is more appropriate for short snippets. Inserting a random affiliate code into a URL, for example, or displaying a random name. It can also be used for displaying random images and other short content.
Below is the word "Randomly" published in a random color. The page may be reloaded to have the software publish another color. (Because this is random and not rotation, the same color may be published consecutively.)
The above word published in a random color is accomplished with the Embedded method.
Implementations instructions follow. First, the Imported method and then the Embedded method.
Imported — Content imported from a randomly selected file
First, create the files to be imported. Make a note of their locations on your server.
To determine their locations, start with their URL. Then, remove the http:// and the domain name. Thus, if a file is at http://example.com/imgtags/image22.html then its location is /imgtags/image22.html
For the code, we'll assume the files are /imgtags/image22.html numbered consecutively through /imgtags/image28.html
The example code assumes each file to be imported contains a complete IMG tag to display an image.
<?php $RandomList = array(); // Leave this line as is. // Edit next lines for the file locations to be inserted randomly. $RandomList[] = "/imgtags/image22.html"; $RandomList[] = "/imgtags/image23.html"; $RandomList[] = "/imgtags/image24.html"; $RandomList[] = "/imgtags/image25.html"; $RandomList[] = "/imgtags/image26.html"; $RandomList[] = "/imgtags/image27.html"; $RandomList[] = "/imgtags/image28.html"; // Leave next line as is. readfile($_SERVER['DOCUMENT_ROOT'].$RandomList[rand(0,count($RandomList)-1)]); ?>
As you can see, it is fairly simple to implement. Leave the first and last lines as is. Insert a
$RandomList[] = "_____";
line for the location of each file (replacing the underline with the file location).
That's it.
Paste it into your PHP web page and you're good to go.
Embedded — Random content obtained from within the PHP code itself
We'll assume the content to be randomly published is
<?php $RandomList = array(); // Leave line as is. // Edit next lines to contain the content to be randomly published. $RandomList[] = "Discount: 10%"; $RandomList[] = "Discount: 15%"; $RandomList[] = "Discount: 20%"; $RandomList[] = "Discount: 25%"; $RandomList[] = "Discount: 30%"; $RandomList[] = "Discount: 35%"; $RandomList[] = "Discount: 40%"; $RandomList[] = "Discount: 45%"; // Leave next line as is. echo $RandomList[rand(0,count($RandomList)-1)]; ?>
Leave the first and last lines as is. Insert a
$RandomList[] = "_____";
line for each item of embedded content (replacing the underline with the content to be published).
Pop it into your PHP web page and you're good to go.
Here is a demonstration of the above code:
Discount: 35%
If you are interested in viewing the source code for the random colored text example near the beginning of this article, here it is.
<div style="border:5px dotted black; padding:10px; font-size:24px; line-height:24px; font-weight:bold; text-align:center; font-family:monospace;"> <?php $RandomList = array(); $RandomList[] = '<span style="color:#000; background-color:#fff">Randomly</span>'; $RandomList[] = '<span style="color:#f00; background-color:#fff">Randomly</span>'; $RandomList[] = '<span style="color:#0f0; background-color:#fff">Randomly</span>'; $RandomList[] = '<span style="color:#00f; background-color:#fff">Randomly</span>'; $RandomList[] = '<span style="color:#ff0; background-color:#fff">Randomly</span>'; $RandomList[] = '<span style="color:#0ff; background-color:#fff">Randomly</span>'; $RandomList[] = '<span style="color:#fff; background-color:#000">Randomly</span>'; echo $RandomList[rand(0,count($RandomList)-1)]; ?> </div>
Publishing random content isn't purely random. It is pseudo-random. Over time, selections may be heavily weighted toward one or several items and very light on another. Random content can also deliver identical items consecutively.
Will Bontrager