Reputation: 20059
Consider the following situation..
$var = 'Lots of information';
function go($var) {
// Do stuff
}
Now, when PHP exits the function, does it clear the memory automatically of all local variables within the function or should I be doing:
unset($var);
...within the function on any local variables that store large amounts of data?
Upvotes: 0
Views: 355
Reputation: 76280
It will clear itself inside the function scope. This means that the $var
parameter of the function will no longer exists after the function call.
Notice that $var = 'Lots of information';
is outside the function block therefore will not be cleared automatically. In this case $var
in the global scope and $var
in the function scope are two different things and inside the function block only $var
in the function scope will exists.
Upvotes: 2
Reputation: 2194
This question goes to the concept of Variable Scope. Inside the function, the variables are "contained" and unless declared global, are not related to variables of the same name outside of the function. So if you created a large variable inside a function, and you want to unset() it, you would need to unset() inside the function. This page is important, especially the part about "global" and "static" variables. PHP also has a way to pass a variable by reference using an ampersand in front of the variable name. In that case, the function is operating on the variable itself, not the function's copy of the variable. http://php.net/manual/en/language.variables.scope.php
Upvotes: 0