In-depth analysis of the use of php unset global variables

Source: Internet
Author: User

In actual use, PHP unset global variables can only destroy local variables and cannot implement global variables. Next we will solve this problem in detail. Hope to help you.

Some functions in PHP are literally hard to understand for beginners. You Need To sum up your experience in actual coding to learn about these functions. Today we will introduce some problems encountered in the use of PHP unset global variables.

In PHP, the statement for releasing variables is called unset (since PHP4, unset is no longer a function, but a statement). A problem occurred when unset was used a few days ago, the unset problem is summarized as follows. If you have carefully read the PHP manual, there is no need to read it again. This article is intended for students who have no intention of reading the manual.

The first thing to emphasize is that the PHP unset global variable is no longer a function in PHP. Since it is not a function, no return value is returned, therefore, the unset return value cannot be used for determination.

Second, in the function, PHP unset global variables can only destroy local variables, and cannot destroy global variables. Let's look at an example in the following manual.Copy codeThe Code is as follows: <? PHP
Function destroy_foo (){
Global $ foo;
Unset ($ foo );
}
$ Foo = 'bar ';
Destroy_foo ();
Echo $ foo;
?>

The returned result is
Bar
Why? 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 is also very simple. It is implemented using the $ GLOBALS array. See the following example:Copy codeThe 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 data in the memory cannot be released during actual operations. In this article, we will explain how to solve the problems.

When we are learning the PHP language, the usage of various functions is usually vague, so we can master them one by one. However, we must master these functions. The following describes how to use the PHP unset () function in detail.

The PHP unset () function is used to destroy a variable. However, in many cases, this function only destroys the variable. The value of the variable stored in the memory is still not destroyed, that is, it fails to achieve the desired effect of releasing memory. Here, we recommend that you use the $ variable = null method to release its memory. The reason is as follows.

The following are some important points about the PHP unset () function: (The following are all tested in windows, php 2.5.9)
This function only releases memory when the space occupied by variable values exceeds 256 bytes.
The address will be released only when all the variables pointing to this value (for example, a referenced variable pointing to this value) are destroyed (the judgment of 1 is also required)
The following is an example of code Demonstration:Copy codeThe Code is as follows: <? Php
$ Test = str_repeat ("1", 256); // repeat a string. The returned value is a string consisting of duplicates.
$ S = memory_get_usage ();
// This function is used to view the memory currently used
Unset ($ test );
$ E = memory_get_usage ();
Echo 'release memory: '. ($ s-$ e );
// The output is 272, but if the above test variable is changed to $ test = str_repeat ("1", 255), the output is 0. If the value of the variable is less than 256, the memory will not be released.
?>

As for why it is 272 instead of 256, it is not very clear and does not know how to handle it internally.Copy codeThe Code is as follows: <? Php
$ Test = str_repeat ("1", 256 );
$ P = & $ test;
Unset ($ test );
Echo $ p;
// The output value is 256. If the above is changed to unset ($ p), even worse, echo $ test is directly displayed as 256 1
?>

That is to say, the value assigned to $ a in the memory still exists. It can be seen that unset () does not release the memory.
However, if $ test = null is added to the above Code or an unset ($ p) is added, the memory can be released. The PHP unset () function test code is as follows:
To assign a value to a variable, use the following method:
Copy codeThe Code is as follows: <? Php
$ Test = str_repeat ("1", 256 );
$ P = & $ test;
$ S = memory_get_usage ();
$ Test = null;
Unset ($ test); // try to replace the order of the sentence with $ test = null, and the result will be different
$ E = memory_get_usage ();
Echo 'release memory: '. ($ s-$ e );
// The output is 272.
Var_dump ($ p); // The output is NULL.
?>

To destroy all the variables that point to the value of this address:Copy codeThe Code is as follows: <? Php
$ Test = str_repeat ("1", 256 );
$ P = & $ test;
$ S = memory_get_usage ();
// Note that the following two unset () orders do not affect the call results.
Unset ($ p );
Unset ($ test );
$ E = memory_get_usage ();
Echo 'release memory: '. ($ s-$ e); // The output is 272.
?>

The PHP unset () function has been demonstrated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.