Php 随机化字符串选择
是否有一种更优雅或更有效的方法来随机选择列表中的字符串Php 随机化字符串选择,php,random,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 = 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;
}
将消息字符串放入一个数组中,然后您可以使用一行代码执行此操作。值得注意的是,
array\u rand()
返回一个随机数组键,而不是一个随机数组值。。。
$messages = array(
"Check this out.",
"Dig this.",
"Listen to this.",
"Try out this."
);
$message = $messages[array_rand($messages)];