PHP - call_user_func_array() 函式



call_user_func_array() 函式使用一個引數陣列來呼叫使用者定義的函式。

語法

mixed call_user_func_array( callback function [, array param_arr])

call_user_func_array() 函式可以使用 `param_arr` 陣列中的引數呼叫自定義函式 `"function"`。

示例 1

<?php
    $func = "str_replace";
    $params = array("monkeys", "giraffes", "Hundreds and thousands of monkeys\n");
    $output_array = call_user_func_array($func, $params);
    echo $output_array;
?>

輸出

Hundreds and thousands of giraffes

示例 2

<?php
   function Box($width,$height, $depth) {
      $b = $width*$height*$depth;
      echo $b;
   }
   call_user_func_array("Box", array("width" => 10, "height" => 20, "depth" => 30));
?> 

輸出

6000

示例 3

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

輸出

1

示例 4

<?php 
   function func($a, $b){
      echo $a."\r\n";
      echo $b."\r\n";
   }
 
   call_user_func_array("func", array(3, 4)); // Different from call_user_func, only the way the parameters are passed is different
?>

輸出

3
4
php_function_reference.htm
廣告