Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are variables used in PHP functions automatically unset after function execution?

I have a question regarding the variables/arrays used in PHP functions. After executing the function, are all the variables automatically unset? If not, when do they unset exactly, after executing the whole PHP page? After a certain time?

Is it useful to unset all variables used in a function at the end of the function to release from memory?

Thank you in advance for your help and comments!

like image 616
Martin Avatar asked Apr 06 '11 03:04

Martin


Video Answer


2 Answers

Local variables that are defined and used in a function are not automatically unset after the function is executed. Rather they are marked for collection by the garbage collector. Unless you are consuming large amounts of memory with the definition of a local variable there really isn't a need to explicitly unset them. Just let the garbage collector do its job.

like image 57
Wes Avatar answered Sep 25 '22 06:09

Wes


Yup, anything not declared a global INSIDE a function will not exist outside the function. Once the function executes, the values are no longer in mem.

PHP: Variable Scope

like image 32
Matthewhall58 Avatar answered Sep 25 '22 06:09

Matthewhall58