timothyarden Posted February 28, 2013 Share Posted February 28, 2013 Hi Everyone, I have a function for which I will always have at least two parameters; function myFunction($type,$value1){ // code } However I would like to be able to have $value 2 and more afterwards (unlimited amount) that I could then use a foreach loop to cucle through it. Is there any way of doing this. I know that the MySQLi classes function bind_param() can do this. If you know how to do this could you please advise me on how. Thanks for reading and for any help in advance, Timothy Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 28, 2013 Share Posted February 28, 2013 You could send the function an array, or use func_get_args Quote Link to comment Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Author Share Posted February 28, 2013 Hi Jessica, Thanks for the fast response. How would I then put some which have correct values those into another function if I used func_get_args()? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 28, 2013 Share Posted February 28, 2013 Uhm, what? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 You mean if you want to pass those same unknown number of arguments to another function? Like func($args[0], $args[1], $args[2], ...) Quote Link to comment Share on other sites More sharing options...
trq Posted February 28, 2013 Share Posted February 28, 2013 You mean if you want to pass those same unknown number of arguments to another function? Like func($args[0], $args[1], $args[2], ...) or call_user_func_array('func', $args); Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 28, 2013 Share Posted February 28, 2013 There is also a technique for variable parameters called "Currying" you can look into. However, I have to question what the actual problem you are trying to solve is. PHP is already loosely typed and you could pass an array which contained the parameters, and use foreach() to go through them. Quote Link to comment Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Author Share Posted February 28, 2013 Hi, Thanks for all the responses. My problem is I want to create a function that takes two mandatory parameters & then a unlimited amount of non-mandatory parameters function test($mand1,$mand2){ } When I call to this function I then want to put in the two mandatory values then have a unlimited amount of non-mandatory paramaters test($mand_param1,$mand_param2,$param3,$param4,$param5); After this I need to cycle through the parameters and be able to put them into another function after the test function validates them. (The test function will also put the params in order) //once validated foreach($parameters_for_test as $value){ // add value to the parameters for if(valid){ // add param to $valid array $valid[] = $value; } } foreach(in $valid){ // add a into the parameters for the next function } so for example the final foreach loop could construct a statment like next_function($a,$b,$c,$d); Thanks for the suggestion of using an array but it wont work to do an array, it must be individual parameters not a load of values bundled into one parameter. I realise that alot of the code above isnt correct but I have wrote it half coding half explaining what I mean. Thanks for everyone's help so far! (Also:The editor here wont allow me to indent the code, sorry) Quote Link to comment Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 Thanks for the suggestion of using an array but it wont work to do an array, it must be individual parameters not a load of values bundled into one parameter.Actually call_user_func_array() that trq mentioned does exactly what you need but you do have to pass it a "load of values bundled into one parameter" Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 28, 2013 Share Posted February 28, 2013 (edited) After this I need to cycle through the parameters and be able to put them into another function after the test function validates them. (The test function will also put the params in order) This sounds very much like you're trying to do too much at once, and thus end up with a lot of unnecessary complexity. As I read it as you're going to pass 2+n arguments to function 1, which then passes n arguments to function two, in the same order. If this is the case, then you should restructure your functions to take 2+1 and 1 argument, respectively. Then run through the intended arguments in a loop, before you call the first function. Then have function 1 return the validated value, which you can then use to call function 2 with. Function 2's task then, will be to build up the finished string, array, or whatever, and then return it to the calling code. So that it can be used outside of the loop. Something like this, in other words: $args = array (1, 3, 6, "test", "foo", "bar"); $query = ''; foreach ($args as $value) { $validated = function_1 ("string", "escape", $value); $query .= function_2 ($validated); } This allows for an unlimited number of "arguments", without all of the above complexity and interdependencies. PS: Indenting can be maintained by either turning off the rich-text editor (top left icon), or using the "paste as plain text" button. Edited February 28, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Author Share Posted February 28, 2013 Okay, I could probably take an array in the first function (that has the second function within it) but then I would have to have each of the values in the array some how iterate themselves into the parenthesis of the second function as individual parameters. Reason being that the second function is the mysqli classes bind_param() Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 (edited) Any ideas?call. user. func. array. [edit] When you use that for bind_param() then you have to build the array by-reference. Edited February 28, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Author Share Posted February 28, 2013 call. user. func. array. Sorry? Quote Link to comment Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Author Share Posted February 28, 2013 Sorry, understood what you meant now (call_user_func_array()) will go check the manual Quote Link to comment Share on other sites More sharing options...
timothyarden Posted February 28, 2013 Author Share Posted February 28, 2013 Okay, thanks heaps, I can see kind of how it works. Ill leave this unsolved for now & see if I can figure out how to work it in. Thankyou Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.