Andrew
Andrew

Reputation: 123

Randomize string selection

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

Answers (1)

Mike B
Mike B

Reputation: 32165

array_rand()

$messages = array(
  "Check this out.",
  "Dig this.",
  "Listen to this.",
  "Try out this."
);

$message = $messages[array_rand($messages)];

Upvotes: 6

Related Questions