Just to elaborate on what Patrick said, in the first case the variables are
temporary, where in the second they persist even after you finish your
loop.  So even after the foreach is finished, the $f1, $f2, and $f3
variables are still storing data- even though it is no longer needed.

In order to free up the memory allocated to these variables you'd have to
unset them after they're used in the loop, or after the loop has finished.

- Mike




On Thu, Mar 15, 2012 at 11:22 AM, Patrick ALLAERT <patrickalla...@php.net>wrote:

> 2012/3/15 Nikita Popov <nikita....@googlemail.com>:
> > If I am understanding the text correctly it is saying that
> >    $f1 = f1();
> >    $f2 = f2($f1);
> >    $f3 = f3($f2);
> > is using more memory than
> >    $f3 = f3(f2(f1()));
> >
> > For me this doesn't make any sense. In the latter case PHP will also
> > create temporary variables to store the return values. There should be
> > no difference in memory consumption.
>
> It does make sense to me.
>
> In the first case, when calling f3(), $f1 is still referenced.
> In the second case, when calling f3(), the result of f2() is
> referenced, but there is no more active reference to the result of
> f1().
>
> Regarding the original problem:
> foreach($a as $key => $val) {
>    $a[$key] = someLong(functionCalls(hereThat($spanOver85Chars)));
> }
>
> Sounds easier to split over lines without temporary zvals:
> foreach($a as $key => $val) {
>     $a[$key] = someLong(
>        functionCalls(
>            hereThat(
>                $spanOver85Chars
>            )
>        )
>    );
> }
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
-----------------------

"My command is this: Love each other as I
have loved you."                         John 15:12

-----------------------

Reply via email to