• PHP Video Tutorials

PHP - call_user_func_array()



The call_user_func_array() function call a user function given with an array of parameters.

Syntax

mixed call_user_func_array( callback function [, array param_arr])

The call_user_func_array() function can call a custom function "function" with the parameters from "param_arr array".

Example 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;
?>

Output

Hundreds and thousands of giraffes

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

Output

6000

Example 3

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

Output

1

Example 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
?>

Output

3
4
php_function_reference.htm
Advertisements