26

Is there a way to call a function using call_user_func and pass it parameters? For instance I have

function test($args)
{
    echo "I DID IT!";
}

however I cannot call this function because it has a paramter which is not being passed by

call_user_func("test");

is there a way to call the function and provide parameters to it? (for instance, the ability to pass in a list arguments)? Any help is greatly appreciated!

2
  • 3
    It takes you less time to write this question than to google "call_user_func"? Commented Aug 1, 2012 at 21:23
  • 2
    @FrancisAvila: googlability of a question does not make it invalid. This site is also supposed to be a reference for others. Commented Aug 1, 2012 at 21:24

2 Answers 2

42

If you were to read the documentation you would see that call_user_func() accepts a variable number of arguments:

call_user_func('test', 'argument1', 'argument2');

You can also use call_user_func_array('callback', array('array','of','arguments')).

Sign up to request clarification or add additional context in comments.

2 Comments

I tried the second solution, but instead of multiple arguments it's going as single argument
Did you change the function name from call_user_func to call_user_func_array? Because it should be passed as multiple arguments.
20

Use call_user_func_array, you can supply a list of parameters as array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.