View Full Version : Pass a function a variable number of arguments by reference

11-03-2006, 12:18 AM
So, is it possible to pass a function a variable number of arguments by reference? Everything I have found says no. One page (http://blog.phpdoc.info/archives/3-PHP-Fun-Variable-Arguments-Be-Reference.html) provided a couple of hackish solutions. This is the function I'm working on by the way. I'm using an array to make it possible, but I would rather not.

function sqlite_escape_list(&$array){
foreach($array as $single){
$single = sqlite_escape_string($single);

11-03-2006, 01:17 AM
seems like the logical way to do it.
not much you can do to get around it.

returning might be a bit complex, but you could put it back into an array or compute to a single value.

this is how one could do a function for an average, for example.

You could also do something similar with a CSV type list, etc., but it would just be more coding.
Might be nice, though, if you wanted to call it like:

The only other option is using up to a specified number of variables, like:
function 3vars($a=0,$b=0,$c=0);
That would allow for no value of 1 or 2 of them and it would use 0, meaning it might have no effect, depending on the math used.
So... you could have "up to" X variables in it.

last thought-- you could use something complex with the eval function to analyze how many varialbes were input and to write, then execute a function depending on that.

11-03-2006, 01:37 AM
returning might be a bit complex, but you could put it back into an array or compute to a single value.

Yes, but the defeats the whole purpose of a referece. The idea of a reference is that when you update the reference the original is updated as well. I know how to pass a variable number of arguments in. What I don't know how to do is pass in a variable number of arguments that are references.

11-03-2006, 02:25 AM
Hmm.... might you just want to repeat the function for each?

The way to do that is the opposite...

as opposed to putting the foreach in the function.

11-03-2006, 03:50 AM
That defeats the purpose too, in this case. My function ideally should allow me to shorten

$name = sqlite_escape_string($name);
$email = sqlite_escape_string($email);
$pass = sqlite_escape_string($pass);
$address = sqlite_escape_string($address);
//and so on

down to just


right now I have it down to


11-03-2006, 08:52 AM

There are many reason the first syntax won't work. Basically.... you must seperately use each variable from the funtion definition in the function.
The way to make that work would be to set like 100 variables in the initial function and type it out manually for each. Then include an "if ([equals default) {[ignore]};" statement for each.
That would allow up to 100.

The only way to do what you're talking about, I think, is the CSV approach and/or using the eval function.
Both would mean, in short, using the syntax of func('$1,$2,...'); and going from there.

But.... arrays will too. why not use arrays?

11-03-2006, 12:20 PM
Well, I also have this written.

function sqlite_escape_list(){
$array = func_get_args();
foreach($array as $single){
$single = sqlite_escape_string($single);

The problem is that I can't figure out how to get that code to work with references.

11-04-2006, 06:16 AM
Ah. Didn't know about those functions.
That might work.

This is pretty much over my head. What I've said so far is the best I can do....

Good luck, and post the final code, if you get it to work :)