OM The Eternity OM The Eternity - 4 months ago 10
PHP Question

Can we Pass an array as parameter in any function in PHP?

i have function to send mail to users and I am willing to pass one of its parameter as an array of ids.

So Is this possible to do, if yes Then how can we do it?

Suppose we have a function as

function sendemail($id,$userid){
Some Process....
}


here $id needs to be array..

Can someone clarify this for me....

Answer

You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside.

function sendemail($id, $userid){
    // ...
}

sendemail(array('a', 'b', 'c'), 10);

You can in fact only accept an array there by placing its type in the function's argument signature...

function sendemail(array $id, $userid){
    // ...
}

You can also call the function with its arguments as an array...

call_user_func_array('sendemail', array('argument1', 'argument2'));