从 PHP 数组中获取随机短语?

问题描述:

我有这样的短语,例如:test 1、test 2、test 3,现在如何在随机模式下显示在加载页面上?

I have this phrases ex: test 1, test 2, test 3, now how to show on load page in random mode?

ex 函数

function random()
{
    $array = ['test 1', 'test 2', 'test 3'];

    return $random_array;
}

将它们放在一个数组中并使用 array_rand 获取随机密钥.

Put them in an array and use array_rand to get a random key.

function random()
{
  $phrases = array(
    'random test 1',
    'random test 2',
    'random test 3'
  );

  return $phrases[array_rand($phrases)];
}