call_user_func_array
Calls a function, passing the arguments as an array.
Syntax
call_user_func_array(callable $callback, array $args): mixedParameters
| Parameter | Type | Description |
|---|---|---|
| $callback | callable | A function or a method. |
| $args | array | The arguments; string keys become named arguments. |
Return value
mixedThe result of the called function.
Examples
Arguments from an array:
<?php
echo call_user_func_array('max', [3, 9, 4]), "\n";
echo call_user_func_array('str_pad', ['7', 3, 'pad_string' => '0', 'pad_type' => STR_PAD_LEFT]);Output:
9
007Notes
The same is usually written more briefly: max(...$numbers).
See also
- call_user_func — Calls a function held in a variable.
- function_exists — Checks whether a function is defined.
- array_map — Applies a function to every element of an array.
In the tutorial
- More about functions — Named arguments, variable-length argument lists, passing by reference, static variables and recursion in PHP.