随机化字符串选择

时间:2012-02-23 19:27:07

标签: php random

是否有更优雅或更有效的方法从列表中随机选择字符串?

$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;
    }

1 个答案:

答案 0 :(得分:6)

array_rand()

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

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