Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Random Variable Echo

I want to use an array to print some words in varying order.

1:16 pm on Jan 19, 2004 (gmt 0)

10+ Year Member


I need help. I want to use 'mt_rand()', to print a series of links in different order. I thought about using an array.

How can I do this so the same link isnt printed twice, or some of the links don't appear.

It's so I can produce a list of links in random order so nobody gets missed out.

Any idea how?


1:44 pm on Jan 19, 2004 (gmt 0)

Hey try this out. Just remember to that arrays start their count at 0 so if you add 10 random links into the array then your rand function should be rand(0,9)

$links= array("www.site1.com","www.site2.com","www.site3.com");
<a href="<? echo ("http://$links[$r]");?>"><? echo $linkT[$r];?></a>

1:54 pm on Jan 19, 2004 (gmt 0)

10+ Year Member

If you want to print the entire list of links randomly, look at using array_rand() to randomize your array before printing it out.
3:36 pm on Jan 19, 2004 (gmt 0)

10+ Year Member

Actually using shuffle() might be a better idea. All you would need to do is to seed the random number generator, shuffle the array, then dump the entire array.

function dump_rand_links ($links) {
srand((float)microtime() * 1000000);
foreach ($links as $link) {
echo "<a href=\"{$link}\">{$link}</a><br />";

This way you don't need to worry about links being missed or duplicated.
12:16 am on Jan 21, 2004 (gmt 0)

10+ Year Member

cheers all :)



Featured Threads

Hot Threads This Week

Hot Threads This Month