• PHP Video Tutorials

PHP - call_user_func() Function



The call_user_func() function can call a user function given by first parameter.

Syntax

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

The call_user_func() function can call a user-defined function given by "function" parameter.

Example 1

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

Output

Hundreds and thousands of giraffes

Example 2

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

Output

0

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

Output

1
2
php_function_reference.htm
Advertisements