Isn't it suitable useful?

I have never seen anyone use unset , and I was wondering if this saves something or if it is a complete waste of time and code?

 <?php $message = "Line 1\r\nLine 2\r\nLine 3"; mail(' admin@example.com ', 'My Subject', $message); /* IS THERE ANY REASON TO UNSET $message ? */ unset($message); ?> 
+4
source share
6 answers

It depends.

If you are developing a long-term daemon or job, unset() can be used to keep memory within reasonable limits: to prevent leaks.

If you are developing a / script page for a web application, unset() not very useful for memory management.

And in any case, useful if you rely on array and you need to remove the value. A silly example, maybe ... 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 provided, this is useless:

 <?php $message = "Line 1\r\nLine 2\r\nLine 3"; mail(' admin@example.com ', 'My Subject', $message); // PHP is about to clean everything up anyway ... don't bother doing this: unset($message); ?> 
+6
source

When passing foreach $value by reference, $value remains in scope after the loop finishes. Stop this 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 
+4
source

There is hardly a reason for this. All memory will be freed when the script completes.

But if you allocate a lot of memory, this can be useful, especially if your scripts take a little longer to run. The memory will be freed immediately and can be used by other scripts.

But, as you say, this requires code, which makes your code less clear. Therefore, do this only if you have allocated large chunks of data that you no longer need, for example, the contents of a file read from disk or large arrays with query results. Don't let go of integers and don't worry about the message line. :)

{edit after comments}

My answer was mainly focused on using PHP for web scripts. Of course, if you create long scripts, especially if the amount of memory that they allocate is very variable, then of course it makes sense to free up memory, although even these scripts work for a limited period of time.

I do not think that in PHP there are many demons that work forever. Most scripts, even those shell scripts, perform a specific task (if you can call "website rendering" or "database migration"), after which they end. In the context of this task, it is still completely useless to free every variable that you have allocated. This is very different from scripts like these and programs that can run for a long time, perform the same task again and again, or are completely controlled by the user, so you don’t know what they will do. And if you manage to write a long script or daemon in PHP, other rules apply because it is a different kind of application. But by then you will not be looking for this answer, because you have already received more than enough knowledge to know what you are doing.

+3
source

Especially useful for arrays. I always used it to remove array elements that I no longer need.

 $arr = array('test', 'No longer needed'); unset($arr[1]); 

But I personally do not find this useful in your sample code.

+3
source

One use for undo is to limit the size of a variable.

 <?php $message = "Line 1\r\nLine 2\r\nLine 3"; mail(' admin@example.com ', '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 in which it is defined. For most PHP variables, there is only one area. This single coverage area included the necessary files as well. For instance:

Here you can learn more about variable fields: http://php.net/manual/en/language.variables.scope.php

Ideally, the variables should be initialized correctly, but if you are not completely sure that you do not enter side effects into the included file, just turn off your variable.

+2
source

I don't know if this fits here, but I used unset () with $ _SESSION variables ...

I believe that I used it to send error messages through pages. When an error is reflected or processed, unset($_SESSION['error']) clears the error message, and the script may continue.

+1
source

Source: https://habr.com/ru/post/1496034/


All Articles