When are PHP variables released?

angryTom
Release: 2023-02-28 06:08:01
Original
3715 people have browsed it

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(&#39;1&#39;,256);
$m=memory_get_usage();
unset($s); //销毁$s
echo $m-memory_get_usage();
?>
Copy after login

Result:272

<?php
$s=str_repeat(&#39;1&#39;,256);
$m=memory_get_usage();
unset($s); //销毁$s
$s = null; //区别在这里,把变量的引用断掉
echo $m-memory_get_usage();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!