Does PHP perform wise memory management in regards to variables within loops?
For example, would memory allocation be performed for the following example, beyond the first iteration?
foreach ($items as $item) { $item_found = true; }
Does PHP perform wise memory management in regards to variables within loops?
For example, would memory allocation be performed for the following example, beyond the first iteration?
foreach ($items as $item) { $item_found = true; }
Memory for variables $items
, $item
and $item_found
will be allocated once (on first iteration only). By default, PHP allocates memory for variables if it encounters new ones.
Read about GC and this question: How does PHP assign and free memory for variables?. It should give you some information about PHP memory management. Also note that PHP uses "copy-on-write" scheme.