-
Display Random Text?
Hi guys, Imagine i have this
$text1 = "hi";
$text2 = "hello";
$text3 = "hey";
$text4 = "Good Day";
How is it possible to display only one of those strings above but also display the ones that have not been chosen for example output would end up like
"Good Day"
we also have Hey,Hello,Hi
so it shows one random output but also displays the remaining that was not chosen randomly elsewhere on the page?
Hope that makes sense
Cheers
-
Re: Display Random Text?
Code:
$texts = array('hi','hello','hey','Good Day');
$text = array_rand($texts);
echo '"' . $texts[$text] . '"<br />we also have ';
unset($texts[$text]);
$ln = count($texts);
for($i=0;$i<$ln;$i++){
if(isset($texts[$i])){
echo ucfirst($texts[$i]) . ($i<$ln-1 ? ',' : '');
}
}
This will do exactly what you typed. It will echo a random element of the array then echo all the other values left over while capitalizing the first letter of each of those values and seperating them by a comma. Right after writing this I realized you could also do:
Code:
implode(',',$texts);
Instead of the loop.
Also if you search "php random array" on Google the first link is to the array_rand() php function.