PHPunset () function destroy variable _ PHP Tutorial

Source: Internet
Author: User
The PHPunset () function destroys the variable. We are learning that the PHPunset () function is used to destroy Variables. However, many times, this function only destroys the variables, and the values of the variables stored in the memory are still not destroyed, that is, we are not learning

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)

1. this function only releases memory when the space occupied by variable values exceeds 256 bytes.
2. 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:

 
 
  1. <? Php
  2. $ Test = str_repeat ("1", 256 );
  3. $ S = memory_get_usage ();
  4. // Modify the function to view the memory currently used
  5. Unset ($ test );
  6. $ E = memory_get_usage ();
  7. Echo 'release memory: '. ($ s-$ e );
  8. // The output is 272, but if the above test variable is changed
    $ Test = str_repeat ("1", 255). The output value is 0.
  9. ?>

As for why it is 272 instead of 256, it is not very clear and does not know how to handle it internally.

 
 
  1. <? Php
  2. $ Test = str_repeat ("1", 256 );
  3. $ P = & $ test;
  4. Unset ($ test );
  5. Echo $ p;
  6. // The output value is 256. If the above is changed to unset ($ p)
    , Even worse, echo $ test is directly displayed as 256 1
  7. ?>

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:

 
 
  1. <? Php
  2. $ Test = str_repeat ("1", 256 );
  3. $ P = & $ test;
  4. $ S = memory_get_usage ();
  5. $ Test = null;
  6. Unset ($ test );
  7. $ E = memory_get_usage ();
  8. Echo 'release memory: '. ($ s-$ e );
  9. // The output is 272.
  10. Var_dump ($ p); // The output is NULL.
  11. ?>

To destroy all the variables that point to the value of this address:

 
 
  1. <? Php
  2. $ Test = str_repeat ("1", 256 );
  3. $ P = & $ test;
  4. $ S = memory_get_usage ();
  5. // Note: The following two unset () sequence does not match
    The relationship does not affect the result.
  6. Unset ($ p );
  7. Unset ($ test );
  8. $ E = memory_get_usage ();
  9. Echo 'release memory: '. ($ s-$ e); // The output is 272.
  10. ?>

The PHP unset () function has been demonstrated.


The reset PHP unset () function is used to destroy the variable. However, in many cases, this function only destroys the variable, and the value of the variable stored in the memory is still not destroyed, that is, no...

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.