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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With