随机化HTML字符串输出

Mar*_*sma 1 html php arrays random wordpress

<?php 
    $random1 = '<p>Quotes van de fans:</p><h2>Why join the navy<BR />if you can be a pirate.</h2><h3>Steve jobs</h3>';
    $random2 = '<p>Quotes van de fans:</p><h2>Lorem2</h2><h3>Steve jobs</h3>';
    $random3 = '<p>Quotes van de fans:</p><h2>Lorem3</h2><h3>Steve jobs</h3>';
    echo(rand($random1, $random2, $random3));
?>
Run Code Online (Sandbox Code Playgroud)

所以我写了上面的代码.我希望我的代码随机化出现的引用.是否有一种用户友好的方式在网站中实现这一点?

我正在使用Wordpress构建一个网站,我想知道Wordpress(或PHP)是否有一种更简单的随机化输出方法.

Kev*_*vin 11

或者,您可以定义一个包含HTML字符串的数组,然后使用array_rand()函数来获取随机条目/元素.例:

// set of elements with random quotes
$quotes = array(
    '<p>Quotes van de fans:</p><h2>Why join the navy<BR />if you can be a pirate.</h2><h3>Steve jobs</h3>',
    '<p>Quotes van de fans:</p><h2>Lorem2</h2><h3>Steve jobs</h3>',
    '<p>Quotes van de fans:</p><h2>Lorem3</h2><h3>Steve jobs</h3>',
);

// apply array_rand function
echo $quotes[array_rand($quotes)];
Run Code Online (Sandbox Code Playgroud)