看书上的例子,头都在看大了,大家举个回调函数的例子出来,我做了半天也没成功!

解决方案 »

  1.   

    echo dde();
    function dde()
    {
       return "ddd";
    }
      

  2.   


    <?php
    function foo(&$bar)//关键是参数前加个&
    {   $bar='2';}
    $str='1';
    foo($str);
    echo $str; //输出2
    ?>网上这种例子很多呀!
      

  3.   

    <?php
      // 此文本是用于 2002 年的,
      // 现在想使其能用于 2003 年
      $text = "April fools day is 04/01/2002\n";
      $text.= "Last christmas was 12/24/2001\n";  // 回调函数
      function next_year($matches) {
        // 通常:$matches[0] 是完整的匹配项
        // $matches[1] 是第一个括号中的子模式的匹配项
        // 以此类推
        return $matches[1].($matches[2]+1);
      }  echo preg_replace_callback(
                  "|(\d{2}/\d{2}/)(\d{4})|",
                  "next_year",
                  $text);  // 结果为:
      // April fools day is 04/01/2003
      // Last christmas was 12/24/2002
    ?> 
      

  4.   


    这个不对!不好意思没注意看!这个事应用全局函数的回调
    function fnCallBack( $msg1 , $msg2 )
    {
        echo 'msg1:'.$msg1;
        echo "<br />\n";
        echo 'msg2:'.$msg2;
    }
    $fnName = "fnCallBack";
    $params = array( 'hello' , 'world' );
    call_user_func_array( $fnName , $params );
    代码说明:    这里使用了PHP内置的函数call_user_func_array来进行调用。call_user_func_array有两个参数,第1个参数是一个字符串,表示要调用的函数名,第2个参数是一个数组,表示参数列表,按照顺序依次会传递给要调用的函数。
    输出:
    msg1:hello
    msg2:world