Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

call_user_func

Calls a function held in a variable.

Syntax

call_user_func(callable $callback, mixed ...$args): mixed

Parameters

ParameterTypeDescription
$callbackcallableA function name, an anonymous function or an "object and method" array.
...$argsmixedThe 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.2025

The same more briefly — calling the variable directly:

<?php
 
$function = 'strtoupper';
 
echo $function('hello');

Output:

HELLO

Notes

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

In the tutorial

Thanks for reading!

Donate to the site — we work hard for you.