2009-12-09 5 views
0

Jouer avec générer du texte au hasard avec chaque rafraîchissement de la page en utilisant php. Y a-t-il une façon plus propre d'aborder cela? Aussi, cela peut-il être fait avec jquery?Nettoyeur PHP Texte au hasard

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

Répondre

8

Utilisez 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

être conscient que array_rand renvoie la clé du tableau, pas la valeur. – Galen

+1

Oui, devrait être print_r ($ random_text [array_rand ($ random_text)]); –

+0

Fonctionne très bien, merci pour l'aide! – Davey