Re: Call by reference to function arguments
| From: | David Otton | Date: | Mon, 07 Jul 2003 17:20:42 +0000 |
| Subject: | Re: Call by reference to function arguments | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-154208@lists.php.net to get a copy of this message | ||
On Mon, 7 Jul 2003 17:36:26 +0100, you wrote:
>I want to write a function (as I have written in several other languages) that
>obtains it's arguments dynamically (using func_get_arg()) and then assigns to that
>argument. Think of the way that scanf() works -- that sort of thing.
>
>I have distilled what I want to do in the code below. foo adds 1 to all of it's arguments.
>
>function foo () {
> $count = func_num_args();
> for($i = 0; $i <= $count; $i++) {
> $var = func_get_arg($i);
> // The following line should do it, but throws a syntax error
> &$var = $var + 1;
> }
>}
>
>$a = '1';
>$b = '2';
>$c = '3';
>
>foo($a, $b, $c);
>
>What I am doing is quite different than the above (and more complex), but I will be able
>to achieve what I want if I can get the above to work.
Well.... this would work
function foo () {
$count = func_num_args();
for($i = 0; $i < $count; $i++) {
$var = func_get_arg($i);
$var = $var + 1;
echo ($var);
}
}
But I get the feeling you're trying to modify the elements in-place? Any
particular reason?
If it's because you want to return multiple values from the function,
consider this:
function foo () {
$args = array();
for ($i = 0; $i < func_num_args(); $i++) {
$args[] = func_get_arg($i);
}
$args = array_map (create_function ('$a','return($a+1);'), $args);
return ($args);
}
list ($a, $b, $c) = foo(1, 2, 3);
echo ("$a, $b, $c");