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?
asked Feb 25, 2009 at 6:33
alexalex
467k197 gold badges865 silver badges975 bronze badges
It was mentioned in the unset manual's page in 2009:
unset[]
does just what its name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.If you are doing
$whatever = null;
then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.
[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:
$a = str_repeat['hello world ', 100];
unset[$a];
var_dump[$a];
Outputs:
Notice: Undefined variable: a in xxx
NULL
But when
$a = null
is used:
$a = str_repeat['hello world ', 100];
$a = null;
var_dump[$a];
Outputs:
NULL
It seems that
$a = null
is a bit faster than itsunset[]
counterpart: updating a symbol table entry appears to be faster than removing it.
- when you try to use a non-existent [
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.] - A variable with null assigned to it is still a perfectly normal variable though.
Al Foиce ѫ
4,05512 gold badges37 silver badges46 bronze badges
answered Feb 25, 2009 at 6:50
VonCVonC
1.2m492 gold badges4145 silver badges4890 bronze badges
13
unset
is not actually
a function, but a language construct. It is no more a function call than a return
or an include
.
Aside from performance issues, using unset
makes your code's intent much clearer.
answered Feb 25, 2009 at 8:55
Alex BarrettAlex Barrett
15.7k3 gold badges50 silver badges51 bronze badges
7
By doing an unset[] on a variable, you've essentially marked the variable for 'garbage collection' [PHP doesn't really have one, but for example's sake] so the memory isn't immediately available. The variable no longer houses the data, but the stack remains at the larger size. Doing the null method drops the data and shrinks the stack memory almost immediately.
This has been from personal experience and others as well. See the comments of the unset[] function here.
I personally use unset[] between iterations in a loop so that I don't have to have the delay of the stack being yo-yo'd in size. The data is gone, but the footprint remains. On the next iteration, the memory is already being taken by php and thus, quicker to initialize the next variable.
answered Feb 25, 2009 at 6:45
William HolroydWilliam Holroyd
3,3441 gold badge20 silver badges25 bronze badges
3
Per that it seems like "= null" is faster.
PHP 5.4 results:
- took 0.88389301300049 seconds
- took 2.1757180690765 seconds
PHP 5.3 results:
- took 1.7235369682312 seconds
- took 2.9490959644318 seconds
PHP 5.2 results:
- took 3.0069220066071 seconds
- took 4.7002630233765 seconds
PHP 5.1 results:
- took 2.6272349357605 seconds
- took 5.0403649806976 seconds
Things start to look different with PHP 5.0 and 4.4.
5.0:
- took 10.038941144943 seconds
- took 7.0874409675598 seconds
4.4:
- took 7.5352551937103 seconds
- took 6.6245851516724 seconds
Keep in mind microtime[true] doesn't work in PHP 4.4 so I had to use the microtime_float example given in php.net/microtime / Example #1.
answered Nov 26, 2012 at 4:12
5
It works in a different way for variables copied by reference:
$a = 5;
$b = &$a;
unset[$b]; // just say $b should not point to any variable
print $a; // 5
$a = 5;
$b = &$a;
$b = null; // rewrites value of $b [and $a]
print $a; // nothing, because $a = null
Antti29
2,91512 gold badges36 silver badges36 bronze badges
answered Aug 2, 2012 at 2:32
RiaDRiaD
45.8k10 gold badges75 silver badges119 bronze badges
2
It makes a difference with array elements.
Consider this example
$a = array['test' => 1];
$a['test'] = NULL;
echo "Key test ", array_key_exists['test', $a]? "exists": "does not exist";
Here, the key 'test' still exists. However, in this example
$a = array['test' => 1];
unset[$a['test']];
echo "Key test ", array_key_exists['test', $a]? "exists": "does not exist";
the key no longer exists.
answered Mar 26, 2011 at 11:03
aurisauris
6816 silver badges5 bronze badges
Regarding objects, especially in lazy-load scenario, one should consider garbage collector is running in idle CPU cycles, so presuming you're going into trouble when a lot of objects are loading small time penalty will solve the memory freeing.
Use time_nanosleep to enable GC to collect memory. Setting variable to null is desirable.
Tested on production server, originally the job consumed 50MB and then was halted. After nanosleep was used 14MB was constant memory consumption.
One should say this depends on GC behaviour which may change from PHP version to version. But it works on PHP 5.3 fine.
eg. this sample [code taken form VirtueMart2 google feed]
for[$n=0; $ngetProductSingle[[int]$ids[$n],true, true, true];
...
answered Nov 19, 2012 at 20:23
OSPOSP
1,4091 gold badge14 silver badges15 bronze badges
For the record, and excluding the time that it takes:
PHP 7.1 Outpu:
took 0.16778993606567 seconds took 0.16630101203918 seconds
answered Jun 27, 2018 at 5:54
Code example from comment
echo "PHP Version: " . phpversion[] . PHP_EOL . PHP_EOL;
$start = microtime[true];
for [$i = 0; $i < 10000000; $i++] {
$a = 'a';
$a = NULL;
}
$elapsed = microtime[true] - $start;
echo "took $elapsed seconds" . PHP_EOL;
$start = microtime[true];
for [$i = 0; $i < 10000000; $i++] {
$a = 'a';
unset[$a];
}
$elapsed = microtime[true] - $start;
echo "took $elapsed seconds" . PHP_EOL;
Running in docker container from image php:7.4-fpm
and others..
PHP Version: 7.4.8
took 0.22569918632507 seconds null
took 0.11705803871155 seconds unset
took 0.20791196823121 seconds null
took 0.11697316169739 seconds unset
PHP Version: 7.3.20
took 0.22086310386658 seconds null
took 0.11882591247559 seconds unset
took 0.21383500099182 seconds null
took 0.11916995048523 seconds unset
PHP Version: 7.2.32
took 0.24728178977966 seconds null
took 0.12719893455505 seconds unset
took 0.23839902877808 seconds null
took 0.12744522094727 seconds unset
PHP Version: 7.1.33
took 0.51380109786987 seconds null
took 0.50135898590088 seconds unset
took 0.50358104705811 seconds null
took 0.50115609169006 seconds unset
PHP Version: 7.0.33
took 0.50918698310852 seconds null
took 0.50490307807922 seconds unset
took 0.50227618217468 seconds null
took 0.50514912605286 seconds unset
PHP Version: 5.6.40
took 1.0063569545746 seconds null
took 1.6303179264069 seconds unset
took 1.0689589977264 seconds null
took 1.6382601261139 seconds unset
PHP Version: 5.4.45
took 1.0791940689087 seconds null
took 1.6308979988098 seconds unset
took 1.0029168128967 seconds null
took 1.6320278644562 seconds unset
But, with other example: