PHP中的垃圾回收如何工作?即,如何清除局部函数变量? [英] How does garbage collection work in PHP? Namely, how do local function variables get cleaned up?

查看:105
本文介绍了PHP中的垃圾回收如何工作?即,如何清除局部函数变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我为未在函数内部声明为全局变量的变量赋值,则该变量会在函数终止时自动取消设置,还是仅在PHP脚本完成执行后才取消设置?

If I assign a value to variable that is not declared global inside a function will that variable be unset automatically when function terminates or will it only be unset when the PHP script finishes executing?

我正在尝试确定手动设置函数内的临时作用域范围变量还是更聪明,因为它会被PHP引擎自动取消设置,因此不必担心.

I'm trying to determine if it's smarter to unset temporary, function scope variables within the function manually or to not worry about because they will be automatically unset by the PHP engine.

推荐答案

该变量将在函数退出时取消设置,除非该函数具有外部引用,否则该变量将保持活动"状态.变量占用的实际内存是否被释放完全取决于垃圾收集器. GC是一项昂贵的操作,PHP仅在需要时才调用它(例如,内存越来越紧).

The variable will be unset when the function exits, unless it has external references to it which would keep it "alive". Whether the actual memory the variable occupied is freed or not is entirely up to the garbage collector. GC is an expensive operation, and PHP will only invoke it when needed (e.g. memory's getting tight).

这篇关于PHP中的垃圾回收如何工作?即,如何清除局部函数变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆