In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the unset global variable in php, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
PHP unset global variables can only destroy local variables in practical use, and can not achieve the purpose of global variables. Next, let's specifically solve this problem. I hope it will be helpful to all of you.
Some functions in PHP language are literally difficult for beginners to understand, so we need to sum up experience in actual coding to master this knowledge. What we are going to introduce to you today are some of the problems encountered in the use of PHP unset global variables.
There is a statement in PHP that releases variables called unset (since PHP4, unset is no longer a function, but a statement). There was a problem with unset a few days ago, so the unset problem is summarized as follows. If you have read PHP's manual carefully, there is no need to read this article, which is for students who don't pay attention to it.
The first point to emphasize is that the PHP unset global variable is no longer a function in PHP, since it is not a function, then there is no return value, so you can not use the return value of unset to make a judgment.
Secondly, in a function, PHP unset global variables can only destroy local variables, not global variables. Let's take a look at an example in the manual.
The copy code is as follows:
< ?PHP function destroy_foo() { global $foo; unset($foo); } $foo = 'bar'; destroy_foo(); echo $foo; ?>The returned result is
Bar
What causes it? The reason is that PHP unset global variables can only destroy local variables in the function. What should I do if I need to destroy global variables in the program? It's also simple, using the $GLOBALS array. Look at the following example:
The copy code is as follows:
< ?PHP function foo() { unset($GLOBALS['bar']); } $bar = "something"; foo(); var_dump($bar); ?>The PHP unset () function is used to destroy variables, but it is often impossible to free the data in memory in practice. In the article, we will explain the solutions to the relevant problems in detail.
When we learn the PHP language, the usage of each function is usually vague, so we can master it one by one. But we have to master these functions. Next we will give you a detailed introduction to the use of the PHP unset () function.
The PHP unset () function is used to destroy variables, but most of the time, this function only destroys the variables, and the value of the variable stored in memory is still not destroyed, that is, it does not achieve the desired effect of freeing memory. Here I suggest that you use the $variable = null to free its memory. The reason will be known by reading the following.
The following are some key points about the PHP unset () function: (the following are all tested in windows, php 2.5.9)
This function frees memory only when the variable value occupies more than 256 bytes long.
Only when all variables pointing to the value (for example, a reference variable pointing to the value) are destroyed, the address will be released (also perform the judgment of 1)
Here is an example code demonstration:
The copy code is as follows:
< ?php $test=str_repeat("1",256); //重复一个字符串,返回值为重复后组成的字符串 $s = memory_get_usage(); //该函数用来查看当前所用内存 unset($test); $e = memory_get_usage(); echo ' 释放内存: '.($s-$e); //输出为272,但如果上面test变量改为$test=str_repeat("1",255),输出则为0 ,变量值不足256不会释放内存的 ?>As for why it is 272 instead of 256, it is not very clear. I do not know how it is handled internally.
The copy code is as follows:
< ?php $test = str_repeat("1",256); $p = &$test; unset($test); echo $p; //输出为256个1。如果上面改为unset($p),更不行了,echo $test 直接显示为256个1 ?>That is, the value assigned to $an in memory still exists. You can see that unset () does not achieve the effect of freeing memory.
However, if you add $test=null to the above code, or add another unset ($p), you can achieve the effect of freeing memory. The test code for the PHP unset () function is as follows:
Variable is assigned to the null method:
The copy code is as follows:
< ?php $test = str_repeat("1",256); $p = &$test; $s = memory_get_usage(); $test = null; unset($test); //试一下将该句与$test=null 调换先后顺序,则结果将不相同 $e = memory_get_usage(); echo ' 释放内存: '.($s-$e); //输出为272 var_dump($p); //输出为NULL ?>The method that destroys all variables pointing to the value in this address:
The copy code is as follows:
< ?php $test = str_repeat("1",256); $p = &$test; $s = memory_get_usage(); //注意,以下2个unset()顺序对调没有关系,不影响结果 unset($p); unset($test); $e = memory_get_usage(); echo ' 释放内存: '.($s-$e); //输出为272 ?>At this point, the argument of the PHP unset () function is complete.
Thank you for reading this article carefully. I hope the article "how to use unset global variables in php" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.