2

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; }

1 Answers1

1

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.

Community
  • 1
  • 1
BlitZ
  • 12,038
  • 3
  • 49
  • 68