["Followup-To:" header set to comp.lang.php.]
Tony wrote:
When writing a function that takes in a variable number of parameters and
processing it using func_get_args or a similar technique, I'm assuming that
one is only able to use the pass by value method of parameter passing. Can
someone confirm or deny this for me?
I think you're right.
Below is a trivial example of what I'm talking about. I'm assuming the
function below does nothing useful.
<?php
function squares( )
{
$params=function_get_args();
$numparams= func_num_args();
for ($i=0;$i<$numparams;$i++)
$params[$i]*=$params[$i];
}
?>
But you can use the $GLOBALS variable to pretend you're passing by
reference.
Here's a way
<?php
function squares() {
$parms = func_get_args();
$nparms = func_num_args();
$retval = array(); // return array
for ($i = 0; $i < $nparms; ++$i)
if ($parms[$i]{0} == '$')
$GLOBALS[substr($parms[$i], 1)] *=
$GLOBALS[substr($parms[$i], 1)];
else $retval[] = $parms[$i] * $parms[$i];
return $retval;
}
$a = 6; $b = 7; $c = 8;
$res = squares($a, "$b", '$c');
print_r($res);
echo $c;
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--