Reputation: 61
I never ever saw someone using unset
and I was wondering if that saves something or if it is a completely waste of time and code?
<?php
$message = "Line 1\r\nLine 2\r\nLine 3";
mail('[email protected]', 'My Subject', $message);
/* IS THERE ANY REASON TO UNSET $message ? */
unset($message);
?>
Upvotes: 5
Views: 887
Reputation: 8223
When passing the foreach $value
by reference, $value
is still in scope after the loop has completed. Stop that madness with unset()
.
foreach($array as &$value)
{
$value = "something else"
}
var_dump($value);
// string(5) "thing"
unset($value);
var_dump($value);
// Notice: Undefined variable: value
Upvotes: 5
Reputation: 116110
There is hardly a reason to do so. All memory will be freed when the script terminates.
But, if you allocate a lot of memory it might be useful to do so, especially if your scripts take a little longer to run. The memory will be freed immediately and can be used by other scripts.
But like you say, it takes code to do so, making your code less clear. So only do this if you have allocated large chunks of data that you don't longer need, like file contents read from disk or big arrays with query results. Don't go freeing integers and don't worry about your message string either. :)
{edit after comments}
My answer was mainly focused to using PHP for website scripting. Of course if you are creating long running scripts, especially if the amount of memory they allocate is very variable, then of course it can make sense to free up memory, although even those scripts run for a limited period of time.
I don't think there are many daemons written in PHP that run forever. Most scripts, even those shell scripts, perform a specific task (if you can call 'rendering a website' or 'migrating a database' specific) after which they end. Within the context of that task it is still quite useless to free up every variable you allocated. This is a big different between scripts like this and programs that might run for a long time, perform the same task over and over again or are completely user-controlled, so you don't know what they will do. And if you do manage to write a long running script or daemon in PHP, other rules apply, because it's another kind of application. But by that time, you won't be searching this answer, because you have gained more than enough knowledge already to know what you're doing.
Upvotes: 4
Reputation: 1760
I don't know if it fits here, but I've used unset() with $_SESSION variables...
I believe I was using it for passing error messages through the pages. When the error is echoed or dealt with - unset($_SESSION['error'])
would clear the error message and the script could continue.
Upvotes: 1
Reputation: 2569
One use for unset is for limiting the scope of the variable.
<?php
$message = "Line 1\r\nLine 2\r\nLine 3";
mail('[email protected]', 'My Subject', $message);
/* if you don't unset $message here, it will span across the include below */
unset($message);
include 'library.inc.php';
?>
From the php manual:
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:
You can read more about variable scopes here: http://php.net/manual/en/language.variables.scope.php
Ideally, variables should be initialized properly, but if you wan't to be completely sure that you don't introduce side-effects to the included file, just unset your variable.
Upvotes: 3
Reputation: 14282
It depends.
If you're developing a long-running daemon or job, unset()
can be used to keep memory in reasonable bounds: to help prevent leaks.
If you're developing a page/script for a web application, unset()
isn't terribly useful for memory management.
And in either case, it's useful if you're relying on array
s and you need to remove a value. A silly example, perhaps ... but consider something like this:
$users = getUsers($someCondition);
foreach ($users as $k => $v) {
unset($users[$k]['password_hash']);
}
print json_encode($users);
In the specific example you've given, it's useless:
<?php
$message = "Line 1\r\nLine 2\r\nLine 3";
mail('[email protected]', 'My Subject', $message);
// PHP is about to clean everything up anyway ... don't bother doing this:
unset($message);
?>
Upvotes: 6
Reputation: 8520
Especially useful for arrays. I've allways used it to remove array elements I no longer needed.
$arr = array('test', 'No longer needed');
unset($arr[1]);
But I personally don't think it's useful in your code example.
Upvotes: 4