Jai Jai - 1 year ago 78
PHP Question

Php writing a function with unknown parameters?

How would I go about writing a function in php with an unknown number of parameters, for example

function echoData (parameter1, parameter2,) {
//do something

But when you call the function you can use:

echoData('hello', 'hello2', 'hello3', 'hello'4);

So that more parameters can be sent as the number of parameters will be unknown.

Answer Source


function echoData(){
    $args = func_get_args();

Be aware that while you can do it, you shouldn't define any arguments in the function declaration if you are going to use func_get_args() - simply because it gets very confusing if/when any of the defined arguments are omitted

Similar functions about arguments

  • func_get_arg()
  • func_get_args()
  • func_num_args()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download