Re: foreach and destroying variables for memory saving



Tim | iHostNZ schrieb:
Hi All,

Just to annoy the hell out of you, another thing that has been on my mind
for a while:

I love the foreach ($ar as $k => $v) { ... } construct and use it all the
time. However, I read somewhere that foreach actually uses a copy of $ar
instead of the array itself by reference. Wouldn't it be much more
usefull/efficient, if foreach would use the array by reference? Then one
could change arrays while iterating over them (without having to use the old
fashioned for ($i=0; $i<count($ar); $i++), also this doesnt work for
associative arrays). I know you can do it with while and list somehow, but i
personally find that language construct rather ugly and can never remember
it. Is there another way that any of you use? Please enlighten me.
I just use foreach because its easy, but it might not be the best. However,
it seems to perform good enough for what i've done so far.

Somewhere i also read that one can save a lot of memory by destroying
variables. Is that done with unset, setting it to null or something similar?
So, i take there is no garbage collection in php? I've never actually looked
at the c source code of php. Maybe its time to actually do that. But it
might be easier if someone can answer this from the top of their head.

Thanks for your patience.

Hi Tim,
i can remember me to hear/read that the variables on PHP will be destroy automatically?.

Regards

Carlos
.



Relevant Pages

  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... (normally this is not the case but not sure in php) ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... I make it a habit not to delete entries in a foreach() loop. ... Rather, I build an array of keys I want to delete, and after the loop ends, delete the entries from my delete array. ... I don't know whether an operation like this is guaranteed to work in PHP - I've never seen it documented, ...
    (comp.lang.php)