PHP - call_user_func() 函式



call_user_func() 函式可以呼叫由第一個引數給定的使用者函式。

語法

mixed call_user_func(callback function [, mixed parameter [, mixed ...]])

call_user_func() 函式可以呼叫由“function”引數給定的使用者定義函式。

示例 1

<?php
    $func = "str_replace";
    $output_single = call_user_func($func, "monkeys", "giraffes", "Hundreds and thousands of monkeys\n");
    echo $output_single;
?>

輸出

Hundreds and thousands of giraffes

示例 2

<?php
   error_reporting(E_ALL);
   function increment(&$var) {
      $var++;
   }
 
   $a = 0;
   call_user_func("increment", $a);
   echo $a."\n";
?>

輸出

0

示例 3

<?php 
   function func($a, $b){
      echo $a."\r\n";
      echo $b."\r\n";
   }
 
   call_user_func("func", 1, 2); // The first one is the function name, followed by the parameter list
?>

輸出

1
2
php_function_reference.htm
廣告