Jump to content
Larry Ullman's Book Forums

How Setup Function To Accept Variable # Of Variables?


Recommended Posts

This may not be the right place to post this, forgive me if this should be posted in another location or this has been answered already somewhere else. Please direct me to examples if you know any, thank you.

 

I would like to create a function that accepts a non-static number of variables.

 

 

Normal Function:

 

function myniftyfunction($var1, $var2) {

 

-- do something here --

 

}

 

 

That is a function that accepts two variables. What if I want to send variables that vary in number? Maybe 1 in one instance and 5 in another? The thing is at this point I do not have a maximum number I may send. This is a function I am creating for wide usage for many different scripts and applications.

 

 

I can set a static number by doing something like this:

 

function myniftyfunction($var1 = 0, $var2 = 0, $var3 = 0, $var4 = 0) {....

 

But whatever number of variables I set there, what happens when I have an application that needs more? Yes, I can go in and add there but my question is, is there a way to create this so that I do not have to? Make it stretchy so to speak?

 

I don't know if there is a way to do this but I am curious if Larry or anyone else here has any ideas on how to achieve this.

 

 

Thanks.

Link to comment
Share on other sites

Hi,

 

Yeah, I checked out that link and apparently there is an issue with 5.3+ with that command. I didn't totally understand what it meant about the need to pass variables, sort of extra work seems to me and duplicates setting up static variables seems to me anyway. Though I am not sure 100% what they meant in their note about 5.3.

 

Passing and array or delimited string to parse within a function would be way to go here I guess.

 

Thanks for your response.

Link to comment
Share on other sites

  • 3 weeks later...
 Share

×
×
  • Create New...