如何在PHP中调用随机函数 [英] How to call random Function in PHP
本文介绍了如何在PHP中调用随机函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个功能,我想知道如何随机调用这两个功能。我的意思是,PHP代码将从这两者中随机选择?我该怎么做?
So I have this functions, I was wondering how can I call the two function randomly. I mean, the php code will randomly select from the two? how can I do that?
示例函数
function one() {
echo '
<div id="two-post">
<a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('dos'); ?>
<div class="entry-meta">
<h1><?php the_title(); ?></h1>
<p>By <?php the_author(); ?></p>
</div>
<div class="overlay2"></div>
</a>
</div>
';
}
function two() {
echo '<div class="two">' . wp_trim_words( get_the_content(), 50, '' ) . '</div>';
}
function three() { // function names without "-"
echo '<div class="third">' . the_author() .'</div>';
}
随机选择两个函数的代码
<?php
$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings
call_user_func($functions[array_rand($functions)]);
?>
以上代码无效。想知道是否有人可以提供帮助吗?
The code above doesn't work. Was wonder if someone could help?
推荐答案
您可以这样称呼它:
function one() {
echo '
<div id="two-post">
<a href="' . the_permalink() .'" alt="' . the_title() .'" title="' . the_title() .'">
' . the_post_thumbnail('dos') . '
<div class="entry-meta">
<h1>' . the_title() . '</h1>
<p>By ' . the_author() . '</p>
</div>
<div class="overlay2"></div>
</a>
</div>
';
}
function two() {
echo wp_trim_words( get_the_content(), 50, '' );
}
function three() { // function names without "-"
echo '<div>' . the_author() .'</div>';
}
$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings
$functions[array_rand($functions)](); // call it!
// or
call_user_func($functions[array_rand($functions)]);
这篇关于如何在PHP中调用随机函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文