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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.