Re: Passing by references
| From: | A Complete Luser | Date: | Sat, 30 Dec 2000 00:32:51 +0000 |
| Subject: | Re: Passing by references | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-32256@lists.php.net to get a copy of this message | ||
"Toby Butzon" <php-lists@imawebdesigner.com> writes:
> What you're trying to do _can_ be done using PHP's references.
>
> When PHP parses your code, it builds a array/hash which holds variables by
> name and value (among other things which are insignificant to this
> discussion, such as type, size, etc.). If you create a reference, PHP just
> adds an additional key for the same value (hence the variable's contents are
> not copied, and an alias is created).
>
> If you are creating large variables within functions and want to keep from
> copying the value to a new variable, _do_ return it as a reference AND
> assign the function's return to the variable as a reference and you will not
> be copying the data, but rather just creating an alias.
>
> For example:
>
> function &myFunction() {
> // create variable you don't want to copy around
> return $myVeryLargeVariable;
> }
>
> $myVeryLargeVariable =& myFunction();
Thanks for that. Seems to make some improvement... What you describe
above seems to cover returning by reference. Am I correct? If this is
the case, would the following bit of code :
$x = 2;
$y = 2;
$result =& return_result($x, $y);
print("RESULT is $result");
exit();
function &return_result($x, $y)
{
$result = $x + $y;
return($result);
}
allocate space for $result twice, or just the once?
Also, how would I pass references to x and y to the function instead
of passing x and y? If I pass x and y as I am in this example (and
yes, I know it's brane damiged(tm) because the variables are so small
references don't save me any memory), aren't I allocating memory for
them twice?
Thanks,
--
A Complete Luser <acompleteluser@penguinpowered.org.uk>