Re: Passing an undetermined amount of arguments by reference
| From: | Jim Lucas | Date: | Wed, 04 Feb 2009 20:17:23 +0000 |
| Subject: | Re: Passing an undetermined amount of arguments by reference | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287633@lists.php.net to get a copy of this message | ||
Jay Moore wrote:
> Greetings list.
>
> Say I have a function that escapes a string before being passed to MySQL
> like so:
>
> function escape($id, &$string)
> {
> $string
> }
Use an array as an alternate method of sending/returning data to the second argument.
function escape($id, &$data) {
if ( is_array($data) ) {
foreach ( $data AS $k => $v ) {
escape($id, $v);
$data[$k] = $v;
}
} else {
$data = mysql_real_escape_string($data, $id);
}
}
This would handle any number of nested arrays/datasets.
Hope it helps.
>
> I'm passing $string as a reference so I don't have to reassign it like so:
>
> $foo = escape($id, $foo);
>
> Simply calling
>
> escape($id, $foo);
>
> works just fine.
>
>
> How can I do this if I have a variable number of arguments? I know I
> can use func_get_args(), func_num_args() and so forth to get the
> arguments but can I still pass by reference?
>
> It'd be so much easier to do
>
> escape($id, $a, $b, $c, $d);
>
> Than
>
> $a = escape($id, a);
>
> and so forth.
>
> Does this make sense? Is it possible to do?
>
> Thanks in advance,
> Jay
>
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare