【问题标题】:How to call random Function in PHP如何在 PHP 中调用随机函数
【发布时间】:2014-09-13 05:18:56
【问题描述】:

所以我有这个函数,我想知道如何随机调用这两个函数。我的意思是,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)]);

?>

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

【问题讨论】:

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

标签: php arrays function random


【解决方案1】:

你可以这样称呼它:

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

【讨论】:

  • 谢谢鬼!不幸的是,我尝试了您的代码,我在此行有一个错误: echo '';
  • 你可以在上面看到我修改后的函数。
  • no @KareenLagasca 删除 PHP 的只是回显它,检查我的修订
  • 我删除了 ';但是,错误现在出现在这行 echo '
    ';和 echo '
    ';
  • @KareenLagasca 我修改了我的答案,看看
【解决方案2】:

你可以在这里使用 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;
            }
            }

【讨论】:

    【解决方案3】:

    删除数组中带有函数名称的括号,例如:$a=array("FUNCTION-ONE","FUNCTION-TWO"); 并在调用时添加它们:echo $a[$random_keys[0]]() . "&lt;br&gt;";

    另外,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>";
    ?> 
    

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2021-03-12
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多