Home >Backend Development >PHP Problem >When are PHP variables released?
When are PHP variables released?
PHP variables are released after the program is executed.
If you are finished using it and want to destroy it, you can use unset to destroy and release
Example: unset($str);
unset and null
$var = null;This method only removes the reference, but does not actually release the memory. The unset method is PHP's built-in method of destroying variables and releasing memory.
But it should be noted that if the reference relationship of the variable referenced by unset(); is still there, then this memory will not be released for the time being. Only when all variables refer to this memory area This freed memory area will be released only after all references are broken.
For example:
<?php $s=str_repeat('1',256); $m=memory_get_usage(); unset($s); //销毁$s echo $m-memory_get_usage(); ?>
Result:272
<?php $s=str_repeat('1',256); $m=memory_get_usage(); unset($s); //销毁$s $s = null; //区别在这里,把变量的引用断掉 echo $m-memory_get_usage(); ?>
Result:192
For more PHP related knowledge, please visitPHP中文网!
The above is the detailed content of When are PHP variables released?. For more information, please follow other related articles on the PHP Chinese website!