Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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.

  1. 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.

  2. 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.)

Randomly

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 "Discount: 10%" through "Discount: 45%" in 5% increments. Here is the code.

<?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: 30%

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC