variable number of args by reference<

liunx

Guest
Ok, here is what I want to do. I want to be able to pass ANY number of arguments to a function. I want to be able to edit these values (append something to them, or whatever). Like this:function if_magic_quotes(){
$num_args = func_num_args();
for($i=0;$i<$num_args;$i++){
$temp = func_get_arg($i);
if(!get_magic_quotes_gpc()){
$temp = addslashes($temp);
}
}
}
anyway, it's a rough example of what I want. Then call it like this:if_magic_quotes($var, $var2, $var3, $var4...)bah, I can't read today :P

ok, so that should work right? if you want to append to one then append to the argument liek it shows you are

$new = "this" . func_get_arg (1);

or something.func_get_arg only gets a COPY of the argument...I can't get it to affect the actual variables that are passed to it (in the example $var, $var2, $var3, & $var4)

so it would be like this:
$var = whatever;
$var2 = whatever2;
$var3 = whatever3;
$var4 = whatever4;

***call to the function that appends 'this'***

echo $var."<br />".$var2."<br />".$var3."<br />".$var4;
and that would yield this:thiswhatever
thiswhatever2
thiswhatever3
thiswhatever4it really isn't a copy but you never return the actual variable so you will never see it. if you don't return the variables you will never know what is going on. all the work is being done in the function, you need to return the work you did. get it?the thing is, how can I return say...50 variables if I pass it 50 arguments.

The thing is...isn't there a way to pass a variable by REFERENCE (like in c++ you use an &before the variable) so that a function recieves a pointer to the ACTUAL variable, so that it is modified, rather than receiving the VALUE of the variable?

C++ example:
void two (int& x)
{
x=2;
cout << x << endl;
}

void one()
{
int y=1;
two(y);
cout << y << endl;
}
running one() would output:2
2
BOTH functions return void, but function one passes y to function two by reference, so that function two can modify the ACTUAL y variable.yes, you can. i think its the same syntax: &$x
but you have to declare those in the function declaration, and since you arent declaring your variable as being passed by reference, they cant be. they are passed by value.
the only thing i can think of, is to put all the values in an array, pass the array to the function, and then return the array with addslashes added.nate...you're right. that works like this:function test(&$test){
$test .= 'test';
}

$stuff0 = '0';
test($stuff0);
echo $stuff0;
This outputs: 0test

The thing is, I can't seem to get it to work with a variable # of args. Only with a fixed # designated like this: funcname(&$one, &$two, &$three) etcthats what i said, you have to declare if it is passed by reference in the function declaration. since you arent declaring any variables, they cant be passed by reference.EUREKA! Thanks...I found a way to do it!
watch this!(yeah...I'm a bit proud):function test(&$test){
for($i=0;$i<sizeof($test);$i++){
$test[$i] .= 'test';
}
}

$stuff0 = '0';
$stuff1 = '1';
$stuff2 = '2';
$stuff3 = '3';
$stuff4 = '4';
test($temp = array(&$stuff0, &$stuff1, &$stuff2, &$stuff3, &$stuff4));
echo $stuff0."<br />".$stuff1."<br />".$stuff2."<br />".$stuff3."<br />".$stuff4;
this outputs:0test
1test
2test
3test
4test

Basically, I pass by reference an array, which holds references to ANY # of variables. Then I run through that array and do whatever I want!hooray!
 
Back
Top