I have like 50 strings.
$rand = rand(0,50);
$name[1] = "Jane";
$name[2] = "Marienne";
...
...
...
$name[50] = "Mary";
echo $name[$rand];
I want to echo “Marienne” if $rand==”2″ for example. But the code above is not working. And I don’t want to use if statement because there are too many strings. What do you suggest me ? Thanks.
Your posted code should work if you don’t have “DOH” moments like an uninitialized
$name[0]key.However, if you want to avoid that problem altogether you could use the
array_randdocs function to pick out a (pseudo) random array key:Alternatively, instead of hard-coding in
50as your max in the random range, why not try: