Questions about PHP variable reference

Source: Internet
Author: User
PHP variable reference puzzles I thought I was already very clear about the variable reference and assignment. The result encountered the following problem, which completely overturned my understanding.

 

After $ s is output, it is still Wangchuanbo. isn't it the same memory address? the original variables are uninstalled. Is there a value for the referenced variables ??


Reply to discussion (solution)

Zhang Fei surnamed Zhang Mingfei Zi Yide
That is, Zhang Fei and Zhang Yide are the same person.
You killed Zhang Fei, just erased the name of Zhang Fei from the world, and the man was still wandering around in the title of Zhang Yide.

Zhang Fei surnamed Zhang Mingfei Zi Yide
That is, Zhang Fei and Zhang Yide are the same person.
You killed Zhang Fei, just erased the name of Zhang Fei from the world, and the man was still wandering around in the title of Zhang Yide.

It makes sense. I can understand it, but it is not very thorough. the reference mechanism of php and The Pointer of C are said to be different. what is the essential difference?

I used to think that this is a good way to understand reference and assignment:

In Windows,Copying a file from an edisk to the desktop is assigned a value. no matter which one is modified, the other is not modified.A shortcut on the desktop is a reference. no matter which one is modified, the other will be modified.

I can't explain it here, so it's confusing.

Unset a reference, only disconnects the binding between the variable name and the variable content
$ S = & $ str; in php, it means
Both $ s and $ str point to the same place. C
After unset, $ str cannot find C.
Let me give you another example.
Xiao Zhang heard Xiao Li say that the treasure is on the sea
Xiao Li lost his memories, but Xiao Zhang still knew that the treasure was at sea.

In C ++, the reference means that the memory address is the same.
In the end, it is a constant pointer.

Simply put
The C pointer points to a specific address in the memory.
The Pointer of C has a concept of data type, so the pointer can be involved in calculation.
The php code is not compiled into a machine code, so the memory address is not accessed.
Indeed, the reference mechanism of php is very similar to the pointer of C. If pointer calculation is not performed, it will be at least the same as the pointer.

First, you have to understand the garbage collection mechanism of PHP. unsetting a variable does not erase it from the memory! Garbage collection in PHP is completed by GC!

Unset a reference, only disconnects the binding between the variable name and the variable content
$ S = & $ str; in php, it means
Both $ s and $ str point to the same place. C
After unset, $ str cannot find C.
Let me give you another example.
Xiao Zhang heard Xiao Li say that the treasure is on the sea
Xiao Li lost his memories, but Xiao Zhang still knew that the treasure was at sea.

In C ++, the reference means that the memory address is the same.
In the end, it is a constant pointer.

I tried it for your explanation.

$str = 'WangChuanbo';$s = &$str;$s = 'new';echo $str;


Indeed, the reference variable has been modified, and the original variable has also been modified. Do I have a thorough understanding of the unset () function? the reference can be understood in the above way. ------- in Windows, copying a file from an edisk to the desktop is a value assignment. no matter which one is modified, the other is not modified. creating a shortcut on the desktop is a reference, no matter which one is modified, will the other one be modified?

Reference the reply from franzhong on the 4th floor: unset a reference, only disconnects the binding between the variable name and the variable content
$ S = & amp; $ str; in php, it means
In C ++, the reference means that the memory address is the same.
......
Of course, if you have to understand this
You have a folder fff on drive F, right Jian "send to => desktop shortcut"
Then, change the name to aaa and operate again. then, change the name to bbb.
Open aaa, create a txt file, and open bbb.
This is not the case.

Simply put
The C pointer points to a specific address in the memory.
The Pointer of C has a concept of data type, so the pointer can be involved in calculation.
The php code is not compiled into a machine code, so the memory address is not accessed.
Indeed, the reference mechanism of php is very similar to the pointer of C. If pointer calculation is not performed, it will be at least the same as the pointer.

I have read two blog posts several times and found that everyone has their own opinions, so I want to thoroughly understand this problem,
Reference 1: http://www.nowamagic.net/php/php_ReferenceOperator.php
Many people misunderstand that the reference in php is the same as the pointer in C. in fact, this is not the case and it is quite different. In C language, pointers do not need explicit declarations in addition to the array transfer process, but must be defined by *. in php, pointers to addresses (similar to pointers) the function is not implemented by the user, but by the Zend core. the reference in php adopts the principle of "copy at Write Time", that is, unless a write operation occurs, variables or objects pointing to the same address are not copied.

2. http://blog.csdn.net/woods2001/article/details/7569099

More and more confused

The sixth floor is right. to understand this, you need to understand the PHP garbage collection mechanism.

PHP variables are not the same as C/C ++. PHP variables are stored in the ZVAL mechanism and struct definitions are as follows:
Typedef struct _ zval_struct zval;
...
Struct _ zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint refcount _ gc;
Zend_uchar type;/* active type */
Zend_uchar is_ref _ gc;
};
The zval struct contains four fields, which are described as follows:

Default attribute name description
Refcount _ gc indicates reference count 1
Is_ref _ gc indicates whether to reference 0
Value stores the value of a variable.
Specific type of the type variable

Unset () does not directly destroy the variable. only when refcount is set to 0 will the variable be recycled by PHP's garbage collection mechanism.

$ A = 10;
Xdebug_debug_zval ('A ');
// Output: a: (refcount = 1, is_ref = 0) = 10
$ B = & $;
Xdebug_debug_zval ('A ');
// Output: a: (refcount = 2, is_ref = 1) = 10
$ A = 20;
Xdebug_debug_zval ('A ');
// Output: a: (refcount = 2, is_ref = 1) = 20
Unset ($ B );
Xdebug_debug_zval ('A ');
// Output: a: (refcount = 1, is_ref = 0) = 20


This post will continue to provide additional points. I hope you will speak enthusiastically and join us.

In fact, many people like the underlying implementation of php when explaining the reference. In fact, this is not correct.
The explanation should be explained by simple principles rather than in-depth principles. Otherwise, the more chaotic it is.
Just as no C-language book will use assembly language to explain pointers, because the high-level has not yet understood how the underlying layer can understand

Php references are like pointers in terms of performance. So it's okay to regard it as a "pointer ".
You just don't need to use the reference as a "pointer" to calculate it.

In another way
The pointer is used to access each shop on the commercial street according to the house number, so he can clearly know where the next store is
The quote comes in and out based on the shop sign, so he cannot know who is next to it.
There is no difference between the two.

Learning!

Remember the following:
In memory, the php reference mechanism is + 1, that is, every time one more reference counter is added + 1, unset one is-1.
As long as the variable still has a unit in use (not 0), the valid variable name is still available

I would like to add three points to this knowledge point:
First: Only variables can have addresses and values. if a variable is added before the variable &, you must pass the variable;

$ A = 100; function myfun (& a) {// & must be followed by a variable. if you write 100 directly, you must assign 100 to the variable $, write $ a ++; return $ a;} echo myfun ($ a); // 101. if it is written as myfun (100), it is incorrect.

Second: When UNSET () is used for deletion, only the reference relationship is deleted and no value is deleted.
$ A = "aaaaaaaa"; $ B = & $ a; unset ($ B); // unset ($ B), only deletes the reference relationship, the value echo $ a; // aaaaaaaa is not deleted.

Third: If a new variable is referenced, the reference relationship changes;
    $a="aaaaaaaaa";    $c="ccccccccc";    $b=&$a;       $b=&$c;    var_dump($b);// string(9) "ccccccccc" 


Find a picture for you online.

I am also asking this question.

I am also asking this question.

What do you understand?

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.