Re: Passing by reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PHP uses copy-on-write for by-value passes.  So, if you are not changing
the passed string inside the function there is no copy done on a
pass-by-value and this is actually faster than doing a pass-by-reference.

But, I'd suggest doing ".$myvar." (ie. break out of the quoted string and
concat the var.)

-Rasmus

On Sun, 17 Nov 2002, Peter Beckman wrote:

> I'm building some software that will be passing a string variable which
> could potentially be up to 125K bytes (characters) in size.
>
> I know that passing a reference to the variable will be much more efficient
> than copying the variable:
>
>  copying, as I understand it:
>
>     foo($var)
>     [...later...]
>     function foo ($myvar) { $sql = "insert into table (this) values (\"$myvar\")"; }
>
>  passing by reference, not copying:
>     foo($var)
>     [...later...]
>     function foo (&$myvar) { $sql = "insert into table (this) values (\"$myvar\")"; }
>
> A few questions:
>     1. Am I correct that PHP makes a copy of the variable when I call the
>        function with the variable, since the scope will not be global in
>        the function (unless I declare it such)?
>     2. Will I be saving some CPU cycles and memory by passing by reference?
>     3. Is my pseudo code above correct?  If not, can you show me how one
>        might pass by reference correctly?
>
> I've tested both pieces of code, but can't determine if there is a
> speed/memory difference, and I don't actually have 125K in data handy!
>
> Thanks,
> Peter
> ---------------------------------------------------------------------------
> Peter Beckman            Systems Engineer, Fairfax Cable Access Corporation
> beckman@purplecow.com                             http://www.purplecow.com/
> ---------------------------------------------------------------------------
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux