metterli in un array e scegliere a caso con rand()
. I limiti numerici passati a rand()
sono zero per il valore inferiore, come il primo elemento nell'array e uno in meno rispetto al numero di elementi nell'array.
$array = array($first, $second, $third);
echo $array[rand(0, count($array) - 1)];
Esempio:
$first = 'first';
$second = 'apple';
$third = 'pear';
$array = array($first, $second, $third);
for ($i=0; $i<5; $i++) {
echo $array[rand(0, count($array) - 1)] . "\n";
}
// Outputs:
pear
apple
apple
first
apple
O molto più semplicemente, chiamando array_rand($array)
e passando il risultato di nuovo come una chiave di matrice:
// Choose a random key and write its value from the array
echo $array[array_rand($array)];
fonte
2011-12-30 04:35:01
Oh grazie :) – Frank