Reputation: 123
Is there a more elegant or more efficient method of randomizing the selection of a string from a list?
$case = rand(1,4);
switch ($case) {
case 1: $message = "Check this out.";
break;
case 2: $message = "Dig this.";
break;
case 3: $message = "Listen to this.";
break;
case 4: $message = "Try out this.";
break;
}
Upvotes: 2
Views: 106
Reputation: 32165
$messages = array(
"Check this out.",
"Dig this.",
"Listen to this.",
"Try out this."
);
$message = $messages[array_rand($messages)];
Upvotes: 6