Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like JavaScript's apply function in PHP?

Tags:

In JavaScript, I can use apply to pass an array as arguments to a function:

var f = function (n,m) {},
    args = [1,2];

f.apply(null, args);

I now need to do something similar in PHP i.e. pass an array of items as 'separate' arguments to a function.

Is there any way this can be done?

like image 215
Andreas Grech Avatar asked Aug 14 '11 22:08

Andreas Grech


People also ask

How to javascript function call in PHP?

You can execute Javascript through PHP by calling javascript code/function as a string in PHP and send it to the client browser to execute.

What is $params in PHP?

PHP Parameterized functions are the functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name.


1 Answers

You can use the function call_user_func_array. Simply pass in your function (as a callback, usually a string with the function name), and an array of arguments.

Additional note: for static functions, use forward_static_call_array.

like image 57
jtbandes Avatar answered Jan 03 '23 03:01

jtbandes