Let's say I had three variables:-
$first = "Hello";
$second = "Evening";
$third = "Goodnight!";
How would I echo a random one onto the page, as I would like to have this module in my website sidebar that would change on every refresh, randomly?
Place them into an array and choose from it randomly with rand(). The numeric bounds passed to rand() are zero for the lower, as the first element in the array, and one less than the number of elements in the array.
$array = array($first, $second, $third);
echo $array[rand(0, count($array) - 1)];
Example:
$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
Or much more simply, by calling array_rand($array) and passing the result back as an array key: 
// Choose a random key and write its value from the array
echo $array[array_rand($array)];
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With