PHP unset function So strange

Source: Internet
Author: User
The value passed by unset is a copy of an object or value (such as passing an array, passing a new copy, not a reference), and the original object is destroyed. It's so weird.
The great God is quick to answer this question. I used to use unset, and today I suddenly thought of this problem.
Like what:


at this point will be reported undefined variable error .
As the dance Forest says, this method of destroying variables is likely to reduce the reference count by one.
But do this:


  
   

This time, $a is still destroyed. Error of the reported Undefined variable error.
If you unset drop b it, take a look at the result:


  
   

Just destroyed $b , $ b Although it is a $a reference, but $a not destroyed. Print outhello
So the unset mechanism is not that simple.

Reply content:

The value passed by unset is a copy of an object or value (such as passing an array, passing a new copy, not a reference), and the original object is destroyed. It's so weird.
The great God is quick to answer this question. I used to use unset, and today I suddenly thought of this problem.
Like what:


at this point will be reported undefined variable error .
As the dance Forest says, this method of destroying variables is likely to reduce the reference count by one.
But do this:


  
   

This time, $a is still destroyed. Error of the reported Undefined variable error.
If you unset drop b it, take a look at the result:


  
   

Just destroyed $b , $ b Although it is a $a reference, but $a not destroyed. Print outhello
So the unset mechanism is not that simple.

The first code, A and B, are two different kinds of memory, so unset it out, it doesn't even affect it.
The second paragraph of code b refers to a so that the corresponding zval ref_count+1 is_ref +1 no matter unset off a or b just a reference to the other value has no effect or point to the area

Variable reference count-1, the memory block will not be destroyed as long as there is a reference to that memory block

Update
Yesterday to write an answer to find the server in the naughty maintenance, thought did not send it ...
Tipi has written specific content:
Http://www.php-internals.com/book/?p=chapt03/03-01-00-variables-structure
Variable structure body

Http://www.php-internals.com/book/?p=chapt03/03-06-01-var-define-and-init
Variable assignment and destruction, here is a detailed explanation of the reference count

I think you're saying it's not true.

$o = new stdClass();$o->var = 123;$new_o = $o; //等于$new_o = & $o;unset($new_o);var_dump($o);

The object assignment itself is a reference assignment, but the variable referenced by unset simply destroys the reference and does not destroy the original variable

My understanding is this way.

... Are you sure?

------------Update-------------
Bring it up in the comments.
Unset just breaks the binding between the variable name and the value

Reference:
"This function frees memory only if the value of the variable takes up more than 256 bytes of space"
&&
"The address is freed when all variables that point to that value (such as a reference variable pointing to that value) are destroyed."

unset($a)No matter how you get a $ A (direct assignment, a value, a pass-through), and what you do with $ A $a=true $a=$b $a=&$b (passing a value to another variable $b=$a or address $b=&$a ), it will break a $ A reference and erase $ A to null.
As far as the assigned object is unaffected, the reference count itself is handled well.

Agree with the answer above, if the LZ problem persists, may wish to post your code can better explain the situation.

But I want to use the following code to give the LZ a wake up:

$foo = true;$bar = &$foo;unset($foo);var_dump($bar);  // 结果是true,而不是null

And in turn, the same

$foo = true;$bar = &$foo;unset($bar);var_dump($foo);  // 结果依然是true,而不是null

Can be seen:即时向unset传递一个变量的引用,也不会把该变量销毁
So I'm not quite able to understand how the LZ problem is.

This code I search from Elsewhere, I think it should be able to solve your doubts!
If you unset () a variable passed by reference in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().

  
  
   

The above example will output:

Something
Something

Landlord of the second test, you can output $b try, as did not be destroyed

Refer to this
http://php.net/manual/zh/features.gc.refcounting-basics.php

  • 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.