tags:

views:

179

answers:

1

Playing around with generating text randomly with each page refresh using php. Is there a cleaner way to approach this? Also, can this be done with jquery?

<?php
$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]");
?>
+5  A: 

Use array_rand()

$random_text = array("Random Text 1",
                "Random Text 2",
                "Random Text 3",
                "Random Text 4",
                "Random Text 5");

print_r($random_text[array_rand($random_text)]);
Yacoby
be aware that array_rand returns the key of the array, not the value.
Galen
Yep, should be print_r($random_text[array_rand($random_text)]);
Alix Axel
Works great, thanks for the help!
Davey
Accept the answer then :)
Blair McMillan