如何在PHP中调用随机函数

Kar*_*sca 4 php arrays random function

所以我有这个功能,我想知道如何随机调用这两个函数.我的意思是,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>';
    }
Run Code Online (Sandbox Code Playgroud)

用于随机选择两个函数的代码

<?php

$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings

call_user_func($functions[array_rand($functions)]);

?>
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用.想知道是否有人可以提供帮助?

Kev*_*vin 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)]);
Run Code Online (Sandbox Code Playgroud)