PHP call_user_func_array Usage Guide

The call_user_func_array function can be used to invoke a callback function and pass an array as parameters to that callback function. Its syntax is as follows:

call_user_func_array(callable $callback, array $param_arr): mixed

$callback is the callback function to be called, which can be a function name or an object method; $param_arr is the array of parameters to be passed to the callback function.

Example:

“The teacher asked the students to read the chapter and answer the questions at the end of the book.”

“The students were told by the teacher to read the chapter and respond to the questions at the conclusion of the book.”

function myFunction($param1, $param2) {
    return $param1 + $param2;
}

$callback = 'myFunction';
$params = array(2, 3);

$result = call_user_func_array($callback, $params);
echo $result; // 输出 5

In the example above, we defined a function called myFunction, then used call_user_func_array to invoke the function, passing the parameter array [2, 3] to it, resulting in the output 5.

bannerAds