PHP unset () function destroy variable _php tutorial

Source: Internet
Author: User
We are learning

The PHP unset () function is used to destroy variables, but most of the time, the function only destroys the variable, and the value of the variable stored in the memory is still not destroyed, that is, it does not achieve the effect we want to release the memory. Here I recommend that you use the $ variable =null method to free its memory. The reason to see the following will know.

Here are a few points about the PHP unset () function: (The following are tested in the Windows environment, PHP 2.5.9)

1. The function frees memory only if the value of the variable takes up more than 256 bytes of space
2. The address is freed only if all variables that point to the value (for example, a reference variable points to that value) are destroyed (and a 1 judgment is performed)

Here's an example code argument:

 
 
    1. < ? php
    2. $ test = str_repeat ( "1", 256);
    3. $ s = memory_get_usage ();
    4. //Change function to view the current memory used
    5. unset ($test);
    6. $ e = Memory_get_usage ();
    7. Echo ' Frees Memory: '. ( $s-$e);
    8. //output is 272, but if the test variable above is changed to
      $ test = str_repeat ("1", 255), output is 0

As for why 272 is not 256, it is not very clear, do not know how to deal with the internal.

 
  
  
  1. < ? PHP
  2. $ Test str_repeat("1", "n" );
  3. $ P = & $test;
  4. Unset ($test);
  5. Echo $p;
  6. The output is 256 x 1. If the above is changed to unset ($p)
    And, more than that, the Echo $test shown directly as 256 x 1
  7. ?>

This means that the value assigned to $ A in memory still exists. Visible unset () does not achieve the effect of releasing memory.

However, if you add $test=null to the above code, or add a unset ($p), you can release the memory effect, the PHP unset () function test code is as follows:

The variable is assigned a null 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 ' Free Memory: '. ($s-$e);
  9. Output is 272
  10. Var_dump ($p); Output is null
  11. ?>

method to destroy all variables that point to the value in this address:

 
  
  
  1. < ? php
  2. $ test = str_repeat (" 1 ", 256);
  3. $ p = & $test;
  4. $ s = Memory_get_usage ();
  5. //Note that the following 2 unset () Order reversal
    has a relationship that does not affect the results
  6. unset ($p);
  7. unset ($test);
  8. $ e = Memory_get_usage ();
  9. Echo ' Frees Memory: '. ( $s-$e); Output is 272
  10. ?>

To this PHP unset () function argument is complete.


http://www.bkjia.com/PHPjc/445987.html www.bkjia.com true http://www.bkjia.com/PHPjc/445987.html techarticle We are learning PHP unset () function is used to destroy variables, but many times, this function only to destroy the variable, the memory stored in the value of the variable 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.