Unset will free the memory used by the structure for use elsewhere on the server or in your program. Normally, with variables such as those, this shouldn't be a huge consideration, and most of the time it's not worth using unset. It can, however, come into effect when you have large data structures stored in memory - you can significantly reduce the memory burden of your application by freeing temporaries and the like. Also, it can help you if you run into the memory_limit.
I'd suggest most of the time it's not necessary, but for any large structure, it's probably a good idea. Also, use references as much as possible when working with large data sets.
Actually, the variable in this example is an array with some 60 or so values. ($data[value1], $data[value2], etc), and most of them will have a fair amount of text in them, but should be no more than 65,535 charcters each value.
Anyway, what do you mean by "freeing temporaries"?
For example, something like this contrived, unrealistic example:
$result = $db->getAll($query);
$special_record = $result; // I don't know why record 13 is important, but it is.
Using unset() on $result will free the space occupied by that variable, which was only ever intended to have a temporary purpose. Many times you'll use variables as intermediate storage during a process, and that can leave copies of large sets of data you don't need floating around.
That's quite a large array, I'd unset it when you're done with it, unless that's just at the end of your script.
Ahh, in that case I avoid using temporary variables if at all possible. It makes the code look un-necessarily complicated and it can confuse me easily when I have a bunch of variables with the same data but under a different name.
//For example, I'm more inclined to do this:
//than do to:
$result = $db->sql_query($sql);
Also, using your example, I would have never made result13 into another variable, but I do make sure that the variable does make sense (eg $customter[name], $customer[address], etc).
As of right now, I have unset all "$sql" variables, and my data variables when I'm done working with them.
Thanks again for the replies. So it seems that there is really no real difference in unset()'ing unless using a large variable thats an array.
Originally posted by MGCJerry
Thanks again for the replies. So it seems that there is really no real difference in unset()'ing unless using a large variable thats an array. [/B]
Or an instance of a large object, or a long string (a file buffer, perhaps). In practical use it won't make much of a difference either way since the runtime is generally fairly short.
As for code style, sometimes it makes it a lot less readable to compress 12 function calls into a single line of code . PHP uses copy-on-write, so if you don't actually modify the data, the cost of assigning it to a variable is very low. When the data is changed, a copy is then made to store the new version at a new location. You still need to unset them though, or when the original variable you copied goes out of scope or is otherwise deallocated, there might still be a reference to the data lying around, and PHP will keep it in memory.
Anyway, don't bother unless you think it might be a problem, or you want to ensure that commonly used variables (e.g. $sql) have a consistent state whenever you go to use them.