Random Text

By Will26 on Jul 16, 2005

This snipplet is great if you want to display random text great if you want to display a new tip each time page is refreshed or visited.

Can be altered to do other things such as display random advertisement.

<?
$random_text = array("Random Text 1",
                    "Random Text 2",
                    "Random Text 3",
                    "Random Text 4",
                    "Random Text 5");
srand(time());
$sizeof = count($random_text);
$random = (rand()%$sizeof);
print("$random_text[$random]");
?>

Comments

Sign in to comment.
surio   -  Jun 10, 2016

http://vihuni.com i cant spin text

 Respond  
srisoftwarez   -  Nov 16, 2009

is there any other way to do this ...

 Respond  
bone282   -  May 16, 2009

array_rand is handy for getting random values:

print($random_text[array_rand($random_text)]);


from a file:

$file = file("randomquotes.txt");
print(trim($file[array_rand($file)]));

 Respond  
tye   -  Aug 29, 2005

Using srand() before rand() isn\'t necessary for PHP 4.2.0+. rand() also takes two parameters like this:

rand(min,max)

This code could be simplified to:
print($random_text[rand(0,count($random_text) - 1)]);

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.