I realise the second one avoids the overhead of a function call (update, is actually a language construct), but it would be interesting to know if one is better than the other. I have been using unset()
for most of my coding, but I've recently looked through a few respectable classes found off the net that use $var = null
instead.
Is there a preferred one, and what is the reasoning?
It was mentioned in the unset manual's page in 2009:
(Since 2013, that
unset
man page don't include that section anymore)Note that until php5.3, if you have two objects in circular reference, such as in a parent-child relationship, calling unset() on the parent object will not free the memory used for the parent reference in the child object. (Nor will the memory be freed when the parent object is garbage-collected.) (bug 33595)
The question "difference between unset and = null" details some differences:
unset($a)
also removes$a
from the symbol table; for example:unset
) variable, an error will be triggered and the value for the variable expression will be null. (Because, what else should PHP do? Every expression needs to result in some value.)