NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP Scope in foreach construct.

Brent Baisley brenttech at gmail.com
Mon Jan 26 12:50:38 EST 2009


foreach makes a copy of the array element you are processing. If the
current "value" is large, like another array, then you have 2 large
arrays. Use a for loop instead or use a reference for the value
variable:
foreach ( $array as &$value ) ...

If you are running into memory problems, you probably should rethink
how you are processing the data. You should process it in chunks and
stream it out to the XML file. Then it does matter how large the data
set gets.

You can also just adjust the php memory limit if you haven't done that
already. It defaults to 8MB or 16MB (5.2).

Use memory_get_usage to see where your memory is jumping.

Hope that helps.

Brent Baisley

On Mon, Jan 26, 2009 at 12:27 PM, Anthony Wlodarski
<anthony at thrillist.com> wrote:
> When working with foreach constructs do the functions and variables defined
> get returned to the garbage collector on each iteration or does the foreach
> have to finish execution before things can be returned to the garbage
> collector?
>
> The reason that I ask is because we are generating data for Google Maps and
> they require one very large XML file which keeps blowing our memory size
> definition inside the php.ini file.  What we might end up doing is creating
> the XML file in batches of lets say 500 items or less and then append the
> files manually.
>
> I figured this was an interesting question and I can't find any answers
> anywhere else (even on php.net docs).
>
> -Anthony
> --
> Anthony Wlodarski
> www.thrillist.com <http://www.thrillist.com>
> Web Applications Developer
> 568 Broadway Ste. 605
> New York, NY, 10012
> (o) 646.786.1944
>
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show_participation.php
>



More information about the talk mailing list