2009-12-09 58 views
0

随机生成随机文本,每个页面刷新使用PHP。有没有更好的方法来处理这个问题?另外,这可以用jQuery来完成吗?Cleaner PHP随机文本

<?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]"); 
?> 

回答

8

使用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)]); 
+0

知道array_rand返回数组,而不是价值的关键。 – Galen 2009-12-09 00:27:52

+1

是的,应该是print_r($ random_text [array_rand($ random_text)]); – 2009-12-09 00:43:31

+0

很好,谢谢你的帮助! – Davey 2009-12-09 02:45:20