Jul
16
2008

Show Random Text in PHP

You know up at the top of the blog where it says “What Happened?”. Well, I wanted that to display another link, but only some of the time. So, I used the rand() function in PHP. Want to learn how to do this yourself? Read ahead.

First, we need to create the beginning to our php document.

<?

After this, we want to set our varaiable for the rand() function. In this case, we will use the variable

$rand

So, the line of code will be:

$rand = rand(1,2);

The (1,2) is so we only get the numbers 1 or 2 returned to choose which block of text to display.

Next, we want to set our blocks of text.

if ($rand == 1) {
echo “TEXT NUMBER 1″;
}
if ($rand == 2) {
echo “TEXT NUMBER 2″;
}

Change TEXT NUMBER 1 & 2 to what you want it to display.

Now, you want to close the document.

?>

You now have a random text generator! You can download the script using the button below.

Download Random Text In PHP

Downloaded a total of 28 times

Written by Jeff in: Coding Projects, OPPD |

No Comments »

RSS feed for comments on this post.

Leave a comment

| © 2008 Old-Computer Network | By using this website, you agree to the Legal Notice.