Jump to content

Adding a Uncertain Amount of Parameters to a Function


timothyarden

Recommended Posts

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

Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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" :P
Link to comment
Share on other sites

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 by Christian F.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.