假设我有三个变量: –
$first = "Hello"; $second = "Evening"; $third = "Goodnight!";
将它们放入一个数组中,然后用
原文链接:https://www.f2er.com/php/136149.htmlrand()
随机选择它.传递给rand()的数字边界为零,作为数组中的第一个元素,一个小于数组中元素的数量.
$array = array($first,$second,$third); echo $array[rand(0,count($array) - 1)];
例:
$first = 'first'; $second = 'apple'; $third = 'pear'; $array = array($first,$third); for ($i=0; $i<5; $i++) { echo $array[rand(0,count($array) - 1)] . "\n"; } // Outputs: pear apple apple first apple
或者更简单地说,通过调用array_rand($array)
并将结果作为数组键传回:
// Choose a random key and write its value from the array echo $array[array_rand($array)];