Terry Posted October 4, 2011 Share Posted October 4, 2011 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 More sharing options...
rob Posted October 4, 2011 Share Posted October 4, 2011 You could pass the function an array. Or, you could use func_get_args within the function. http://www.php.net/manual/en/function.func-get-args.php I've also seen functions taking pipe delimited strings "var1|var2|var3" 1 Link to comment Share on other sites More sharing options...
Terry Posted October 6, 2011 Author Share Posted October 6, 2011 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 More sharing options...
Frank Posted October 28, 2011 Share Posted October 28, 2011 function myfunc($options = array()) { if (isset($options['var1']) ... } Link to comment Share on other sites More sharing options...
Recommended Posts