如何在PHP中调用随机函数

4

我有两个函数,我想知道如何随机调用它们。也就是说,PHP代码将从这两个函数中随机选择一个?我该怎么做呢?

示例函数

    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)]);

?>

上面的代码不起作用。想知道是否有人能够帮忙?

函数名不能包含“-”字符。 - Jason Silberman
在 D:\Xampp\htdocs...php 的第 12 行出现错误:函数名必须是字符串。 - Kareen Lagasca
add_shortcode 接受两个字符串参数,因此在可调用的名称周围添加 '' - Jite
你仍然以错误的方式调用函数。你将它们作为大写字符串,仍包括括号。请阅读我的答案。 - Jite
或许您没有使用问题底部的代码?请发布错误信息。(另外,您正在回显函数的结果,但函数本身内部已经有了回显,请从函数调用中删除回显或让函数返回数据而不是回显它)。 - Jite
4个回答

5
您可以称其为类似以下的内容:
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)]);

谢谢 Ghost!我尝试了你的代码,不幸的是,在这一行我遇到了一个错误:echo ''; - Kareen Lagasca
你可以看到我上面修改过的函数。 - Kareen Lagasca
把PHP代码中的内容删除,只留下echo语句,查看我的修改。 - Kevin
我已经移除了 <?php,所以现在的代码是:echo wp_trim_words( get_the_content(), 50, $more = '...' ); ';但是,错误现在出现在这两行代码中:echo '<div><?php the_title(); ?></div>'; 和 echo '<div><?php the_author(); ?></div>'; - Kareen Lagasca
代码有点小问题...带有 <div>' . the_title() . '</div> 的行会返回类似于以下内容:<div></div>TITLE 问题在于 <?php the_title?> 在 <div></div> 外面,应该放在里面对吧? - Kareen Lagasca
显示剩余6条评论

3
您可以在这里使用Switch case...
function one(){
             //some code;
             }
function two(){
             //some code;
             }
function random_caller(){
          int x = rand(0,1);
          switch(x){
          case 1: one();
          break;
          case 2: two();
          break;
          default: echo "could not run any function";
          break;
            }
            }

2

将函数名数组中的括号移除,例如:$a=array("FUNCTION-ONE","FUNCTION-TWO"); 在调用时加上括号:echo $a[$random_keys[0]]() . "<br>";

此外,PHP不支持函数名中包含字符-(不能使用这样的函数名),因此请尝试将函数重命名为类似于functionOne的名称(这也更符合php标准)。

<?php
$functions = array("functionOne","functionTwo");
$function = array_rand($functions); // no second param uses default param which is 1, and will only return one entry.
echo $functions[$function]() ."<br>";
?> 

2

试试这个:

function one()   { echo 'ONE'; }
function two()   { echo 'TWO'; }
function three() { echo 'THREE'; }

$functions = array('one', 'two', 'three');
call_user_func($functions[array_rand($functions)]);

或者在一个函数中:

function callRandomFunction($functions)
{
    call_user_func($functions[array_rand($functions)]);
}

调用方式如下:

callRandomFunction($functions);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接