call_user_func
Calls a function held in a variable.
Syntax
call_user_func(callable $callback, mixed ...$args): mixedParameters
| Parameter | Type | Description |
|---|---|---|
| $callback | callable | A function name, an anonymous function or an "object and method" array. |
| ...$args | mixed | The arguments for the function. |
Return value
mixedThe result of the called function.
Examples
A call by name and a method call:
<?php
echo call_user_func('strtoupper', 'hello'), "\n";
echo call_user_func([new DateTime('2025-01-30'), 'format'], 'd.m.Y');Output:
HELLO
30.01.2025The same more briefly — calling the variable directly:
<?php
$function = 'strtoupper';
echo $function('hello');Output:
HELLONotes
Modern code calls a function from a variable directly: $callback($value). An array of arguments is passed with call_user_func_array() or $callback(...$args).
See also
- function_exists — Checks whether a function is defined.
- array_map — Applies a function to every element of an array.
In the tutorial
- Anonymous functions and closures — Functions as values in PHP — anonymous and arrow functions, the use construct, the callable type.