An in-depth analysis of the application of global variables in PHP unset _php techniques

Source: Internet
Author: User
Tags php language
PHP unset Global variables can only destroy local variables in actual use, and can not achieve the purpose of global variables. Here we will deal with this problem concretely. Hope to help you.

Some functions in the language of PHP are literally hard to understand for beginners, and they need to be summed up in the actual code to master this knowledge. Today we are going to introduce some of the problems that PHP unset global variables encounter in their use.

PHP has a release variable is called unset (starting from PHP4 unset is no longer a function, but a statement), a few days ago with the unset time out a point of the problem, so the unset problem summarized below. If you have carefully read the PHP manual, then there is no need to read this article, this article is written to see the manual not attentively students.

The first thing to emphasize is that the PHP unset global variable in PHP is no longer a function, since it is not a function, then there is no return value, so the time can not be used to unset the return value to make judgments.

Second, in the function, PHP unset global variables can only destroy local variables, and can not destroy global variables, look at the manual of an example
Copy Code code as follows:

<? Php
function Destroy_foo () {
Global $foo;
Unset ($foo);
}
$foo = ' Bar ';
Destroy_foo ();
Echo $foo;
?>

The result returned is
Bar
Why is that? The reason is that the PHP unset global variable can only destroy local variables in a function. What should you do if you need to destroy global variables in your program? It is also very simple to use $globals arrays to implement. Look at the following example:
Copy Code code 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 not possible to release the data in memory in the actual operation. We will explain the solution to the problem in detail in the article.

We are learning the PHP language, usually the use of each function is more vague, it is one by one to master. But we have to master these functions. Here's a detailed description of how PHP unset () functions are used.

The PHP unset () function is used to destroy variables, but in many cases, the function only destroys the variable, and the value of the variable in memory is not destroyed, which means that we are not able to achieve the desired release of memory. Here I suggest you use the $ variable =null method to free its memory. The reason to see the following will know.

Here are some points about the PHP unset () function: (The following are tested in the Windows environment, PHP 2.5.9)
This function frees up memory only if the value of the variable is longer than 256 bytes
The address is released only if all variables pointing to the value (for example, if a reference variable points to that value) are destroyed (and 1 of the judgment is performed).
Here is an example code argument:
Copy Code code as follows:

< PHP
$test =str_repeat ("1", 256); Repeats a string that returns a string consisting of a duplicate value
$s = memory_get_usage ();
This function is used to view the currently used memory
Unset ($test);
$e = Memory_get_usage ();
Echo ' Frees memory: '. ($s-$e);
The output is 272, but if the test variable above is changed to $test=str_repeat ("1", 255), the output is 0, the value of the variable is less than 256 does not release the memory
?>

As for why 272 rather than 256, it is not very clear, do not know how the internal treatment.
Copy Code code as follows:

< PHP
$test = Str_repeat ("1", 256);
$p = & $test;
Unset ($test);
Echo $p;
Output is 256 1. If the above changes to unset ($p), even worse, Echo $test directly displayed as 256 1
?>

This means that the value assigned to $a in memory still exists. Visible unset () does not achieve the effect of freeing memory.
However, if you add $test=null to the above code, or add a unset ($p), you will be able to release the memory effect, the PHP unset () function test code is as follows:
variable assignment to null method:
Copy Code code as follows:

< PHP
$test = Str_repeat ("1", 256);
$p = & $test;
$s = memory_get_usage ();
$test = null;
Unset ($test); Try to change the order of the sentence with the $test=null, the result will be different
$e = Memory_get_usage ();
Echo ' Frees memory: '. ($s-$e);
Output is 272
Var_dump ($p); Output is null
?>

How to destroy all variables that point to values in this address:
Copy Code code as follows:

< PHP
$test = Str_repeat ("1", 256);
$p = & $test;
$s = memory_get_usage ();
Note that the following 2 unset () Order reversal is not related, does not affect the result
Unset ($p);
Unset ($test);
$e = Memory_get_usage ();
Echo ' Frees memory: '. ($s-$e); Output is 272
?>

To this PHP unset () function argument is complete.
Related Article

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.