Home >Backend Development >PHP Tutorial >PHP unset() function destroys variables_PHP tutorial
We are learning
The PHP unset() function is used to destroy variables, but many times, this function only destroys the variables and stores them in the memory. The value of this variable is still not destroyed, which means that the effect of releasing memory that we want has not been achieved. Here I suggest you use the $variable=null method to release its memory. You will know the reason after reading the following.
The following are some key points about the PHP unset() function: (The following are all tested in the Windows environment, PHP 2.5.9)
1. This function only takes up the space occupied by the variable value The memory will be released only when it exceeds 256 bytes in length
2. Only when all variables pointing to the value (such as reference variables pointing to the value) are destroyed, the address will be released (the judgment of 1 must also be performed) )
The following is an example code argument:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">test</span><span>=</span><span class="attribute-value">str_repeat</span><span>("1",256); </span></li><li class="alt"><span>$</span><span class="attribute">s</span><span> = </span><span class="attribute-value">memory_get_usage</span><span>(); </span></li><li><span>//改函数用来查看当前所用内存 </span></li><li class="alt"><span>unset($test); </span></li><li><span>$</span><span class="attribute">e</span><span> = </span><span class="attribute-value">memory_get_usage</span><span>(); </span></li><li class="alt"><span>echo ' 释放内存: '.($s-$e); </span></li><li><span>//输出为272,但如果上面test变量改为<br />$</span><span class="attribute">test</span><span>=</span><span class="attribute-value">str_repeat</span><span>("1",255),输出则为0 </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
As for why it is 272 instead of 256, it is not very clear. I don’t know how it is handled internally.
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">test</span><span> = </span><span class="attribute-value">str_repeat</span><span>("1",256); </span></li><li class="alt"><span>$</span><span class="attribute">p</span><span> = &$test; </span></li><li><span>unset($test); </span></li><li class="alt"><span>echo $p; </span></li><li><span>//输出为256个1。如果上面改为unset($p)<br />,更不行了,echo $test 直接显示为256个1 </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
That is to say, the value assigned to $a in the memory still exists. It can be seen that unset() does not achieve the effect of releasing memory.
But if you add $test=null to the above code, or add an unset($p), you can achieve the effect of releasing memory. The PHP unset() function test code is as follows:
Method of assigning a variable to null:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">test</span><span> = </span><span class="attribute-value">str_repeat</span><span>("1",256); </span></li><li class="alt"><span>$</span><span class="attribute">p</span><span> = &$test; </span></li><li><span>$</span><span class="attribute">s</span><span> = </span><span class="attribute-value">memory_get_usage</span><span>(); </span></li><li class="alt"><span>$</span><span class="attribute">test</span><span> = </span><span class="attribute-value">null</span><span>; </span></li><li><span>unset($test); </span></li><li class="alt"><span>$</span><span class="attribute">e</span><span> = </span><span class="attribute-value">memory_get_usage</span><span>(); </span></li><li><span>echo ' 释放内存: '.($s-$e); </span></li><li><span>//输出为272 </span></li><li class="alt"><span>var_dump($p); //输出为NULL </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
Method of destroying all variables pointing to the value in the address:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">test</span><span> = </span><span class="attribute-value">str_repeat</span><span>("1",256); </span></li><li class="alt"><span>$</span><span class="attribute">p</span><span> = &$test; </span></li><li><span>$</span><span class="attribute">s</span><span> = </span><span class="attribute-value">memory_get_usage</span><span>(); </span></li><li class="alt"><span>//注意,以下2个unset()顺序对调没<br />有关系,不影响结果 </span></li><li><span>unset($p); </span></li><li class="alt"><span>unset($test); </span></li><li><span>$</span><span class="attribute">e</span><span> = </span><span class="attribute-value">memory_get_usage</span><span>(); </span></li><li class="alt"><span>echo ' 释放内存: '.($s-$e); //输出为272 </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
This concludes the demonstration of the PHP unset() function.