PHP 函数中使用的变量在函数执行后会自动取消设置吗?
我有一个关于 PHP 函数中使用的变量/数组的问题。执行函数后,所有变量是否自动重置?如果没有,在执行整个 PHP 页面后,它们什么时候准确地取消设置?一定时间后?
在函数末尾取消设置函数中使用的所有变量以从内存中释放是否有用?
预先感谢您的帮助和评论!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在函数中定义和使用的局部变量在函数执行后不会自动取消设置。相反,它们被标记为由垃圾收集器收集。除非您在定义局部变量时消耗了大量内存,否则实际上不需要显式取消设置它们。让垃圾收集器完成它的工作即可。
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.
是的,任何未在函数内部声明为全局的内容都不会存在于函数外部。一旦函数执行,这些值就不再位于内存中。
PHP:变量作用域
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
是的,一旦它们的作用域结束(函数返回),它们就会被释放。
我不认为它们的内存会自动清除,相反,当需要额外的内存时它们会被清除。
Yes, they are deallocated once their scope is finished with (function returns).
I don't think their memory is automatically cleared though, rather, they will be cleared when additional memory is required.