php - How can I call a function with a dynamic amount of arguments? -


i have array , need pass each element of array parameters function.

say:

$var = array("var2","var3","var4"); //pass each element new parameter function call_to_function ($var2, $var3, $var4);  //however, if number of array element change can $var = array("var2","var3"); call_to_function ($var2,$var3); 

the problem here is, how build dynamic number of parameters , call function.

use: php function mysqli_stmt::bind_param, takes multiple parameters. , need derive parameters arrray.

is there way it?

this should work you:

first need create array references corresponding variables. can foreach loop below:

foreach($var $v) {         $bindvalues[] = &$$v;  } 

after can use call_user_func_array() call function bind variables. here depends if use procedural or oop style:

procedural:

call_user_func_array("mysqli_stmt_bind_param", array_merge([$stmt, $types], $bindvalues)); 

oop:

call_user_func_array([$stmt, "bind_param"], array_merge([$types], $bindvalues)); 

Comments

Popular posts from this blog

javascript - three.js lot of meshes optimization -

smartface.io - Proper way to change color scheme for whole application -

Email notification in google apps script -