Suppose I want to have two variables and have them both equal to null. (More realistically, I am thinking about an array that contains a large amount of nulls, but the “two variables” scenario is sufficient for the question.) Obviously, I can do this in more than one way. I can do this (method 1):
$a = null;
$b = $a;
By my understanding, the result of this is that there is one zval that is pointed to by two entries in the symbol table: 'a' and 'b'. But alternatively one might do this (method 2):
$a = null;
$b = null;
Naively one would expect that this should result in two different zvals, each pointed to by one entry in the symbol table.
Does it follow from this that if you want to have a large array, and many elements of the array will be null, it’s more efficient (in terms of zval/memory use) to create a $master_null variable with the value null, and then write the null elements of the array by assigning using $master_null?
Consider this script:
which on my machine outputs: 21687696, that is 21 MB of used memory. On the other hand using this:
outputs: 13686832, which is 13 MB. Based on this information, you can assume that at far as memory usage is your concern, it is actually better to indeed use the “master null” variable. However you still need to have all the items in the array, and every entry in a HashTable (internal representation of arrays) takes also some memory.
If you want to dig deeper in the zvals and references, I suggest using the function
debug_zval_dump. Using it, you can see, which variables share the same zval:which outputs:
And this implies that although variables $x and $q are both NULL, they are not the same zval. But $x and $y share the same zval, because they are assigned to each other. I believe you know of the function
debug_zval_dump, but if not, make sure you carefully read the refcount explanation at http://php.net/manual/en/function.debug-zval-dump.php.Also at the end of my post, I want to say that this information might be useful for a better knowledge of PHP internals, I think it is quite useless to do any optimizations. Mostly because there are much better places to start optimizing scripts than such micro-optimizations. Also while this is not part of the specification, PHP authors might change this behaviour in the future (e.g. all NULL variables could share the same zval in some future version).