Random two word changer (3 times random)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hugene13
Forum Newbie
Posts: 1
Joined: Mon Nov 01, 2010 8:52 pm

Random two word changer (3 times random)

Post by hugene13 »

Hi all,

PHP noob here; I am wanting to know if anyone could help me out with a little thing I am trying to accomplish.

What I need help is I need to change two words each time a page on an html site is refreshed. At random one of the three, two words will be displayed in a paragraph of text. Like so:

1st Option -
This a text in a paragraph.This a text in a paragraph.This a text in a paragraph. The sky is blue.

2nd Option -
This a text in a paragraph.This a text in a paragraph.This a text in a paragraph. The sky isn't red.

3rd Option -
This a text in a paragraph.This a text in a paragraph.This a text in a paragraph. The sky looks green.

I've done the best I could to explain this, let me know if you have nay more questions!

-Eugene
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Random two word changer (3 times random)

Post by Jonah Bron »

Code: Select all

$word_set_1 = array('is', 'isn\'t', 'looks');
$word_set_2 = array('blue', 'red', 'green');

$word_1 = $word_set_1[rand(0, 2)];
$word_2 = $word_set_2[rand(0, 2)];

echo 'This a text in a paragraph. This a text in a paragraph. This a text in a paragraph. The sky ' . $word_1 . ' is ' . $word_2 . '.';
rand() produces a random number between the first argument (if present) and the second argument (if present).
Post Reply