Last Updated: February 25, 2016
·
1.728K
· yaodong

PHP call_user_func() and call_user_func_array() parameters

The parameters for call_user_func() and call_user_func_array() are not passed by reference.

$function = function (&$p) {
    echo $p;
};
$param = '123';

call_user_func($function, $param);
// or
call_user_func_array($function, [$param]);

You'll get an error message as PHP Warning: Parameter 1 to {closure}() expected to be a reference, value given.

To fix this, you can use this instead:

$function($param);
// or
call_user_func_array($function, [&$param]);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#dongbeta
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside