I thought I had a very clear reference and assignment to the variable, and the result met the following question, completely subverting my understanding.
Results $s output or wangchuanbo, not the same block memory address, the original variables are unloaded, reference variables have value??
Reply to discussion (solution)
Zhang Fei's surname is a flying word wing de
That is, Zhang Fei and Zhang Yide are the same person
You killed Zhang Fei, just put the name of Zhang Fei to erase the world, and that person still in Zhang Yide this title everywhere wandering
Zhang Fei's surname is a flying word wing de
That is, Zhang Fei and Zhang Yide are the same person
You killed Zhang Fei, just put the name of Zhang Fei to erase the world, and that person still in Zhang Yide this title everywhere wandering
Very reasonable, can understand the point, but not very thorough, PHP reference mechanism and C's pointers are said to be different, unclear what is the essential difference?
I used to think it was a good way to understand references and assignments:
In the Windows system, copy a file on the E disk to the desktop is an assignment, no matter which one is modified, the other is not modified , and a shortcut on the desktop is a reference, no matter which one is modified, the other will be modified
It's completely out of the way, so confusing.
Unset a reference, just breaks the binding between the variable name and the variable content
$s = & $str; in PHP, it means
$s and $str all point to the same place, C.
After unset, $str can't find C.
I'll give you another example.
Xiao Zhang listens to Xiao Li and says that the treasure is on the sea
Xiao Li lost his memory, but Xiao Zhang still knows that the treasure is at sea
In C + +, references mean that even memory addresses are the same
After all, it's a constant pointer.
To put it simply
The pointer to C is pointing to a specific address in memory.
The pointer to C has the concept of a data type, so pointers can be involved in the operation
The PHP code is not compiled into machine code, so there is no access to the memory address.
It is true that the reference mechanism of PHP is very similar to the pointer C, if not counting the pointer, then at least in the performance of the pointer is the same
First you have to understand the PHP garbage collection mechanism, you unset out a variable is not erased from memory! The garbage collection of PHP is done by GC!
Unset a reference, just breaks the binding between the variable name and the variable content
$s = & $str; in PHP, it means
$s and $str all point to the same place, C.
After unset, $str can't find C.
I'll give you another example.
Xiao Zhang listens to Xiao Li and says that the treasure is on the sea
Xiao Li lost his memory, but Xiao Zhang still knows that the treasure is at sea
In C + +, references mean that even memory addresses are the same
After all, it's a constant pointer.
In response to your explanation, I experimented.
$str = ' Wangchuanbo '; $s = & $str; $s = ' new '; Echo $str;
It is true that the reference variable has been modified, the original variable is also modified, I do not understand the unset () function, the reference can be understood in the way I do not-------in the Windows system, the E disk copy of a file to the desktop is an assignment, no matter which one is modified, the other will not be modified , and a shortcut on the desktop is a reference, and no matter which one you modify, the other will be modified.
Reference 4 Floor Franzhong's reply: unset a reference, just breaks the binding between the variable name and the variable content
$s = & $str; in PHP, it means
In C + +, references mean that even memory addresses are the same
......
Of course, if you have to understand that, hehe.
You have a folder on the F disk FFF, right-hand "send to =〉 desktop shortcuts"
Then the name is AAA, and then it's called BBB.
Open AAA build a txt inside, and then open the BBB inside also have txt
No, that's what happened.
simply say
C's pointer to a specific address in memory
C pointer has a data type concept, so pointers are can participate in the operation of the
PHP code is not compiled into machine code, so no access to memory address one said
It is true that PHP's reference mechanism is very similar to the pointer to C, if not counting the pointer, at least in the same way as the pointer
There are two blog posts I read several times, I found that everyone has their own views, so I want to get this problem,
Reference:/http www.nowamagic.net/php/php_ReferenceOperator.php
Many people misunderstand that the reference in PHP is the same as the pointer in C, which is not, in fact, a big difference. In addition to the pointer in the C language is not explicitly stated in the process of the array, the others need to be defined using *, and PHP's point-of-address (similar pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP reference in the use of "copy-on-write" principle, is unless a write operation occurs, a variable that points to the same address, or an object that is not copied
Reference II, http://blog.csdn.net/woods2001/article/details/7569099
Find it more and more confusing
6 floor to understand this, you need to understand the garbage collection mechanism of PHP
PHP variables are not stored in the Zval organization as PHP variables are defined as
typedef str UCT _zval_struct Zval;
...
struct _zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint Refcount__g C
Zend_uchar type;/* Active type */
Zend_uchar is_ref__gc;
}; There are four fields in the
zval struct, meaning:
Property name meaning default
REFCOUNT__GC represents the reference count 1
IS_REF__GC Indicates whether the value of the reference 0
value stores the variable
unset () does not directly destroy the variable until refcount=0 is recycled by the PHP garbage collection mechanism
$a = 10;
Xdebug_debug_zval (' a ');
//output:a: (refcount=1, is_ref=0) =10
$b = & $a;
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 add points, I hope that we are enthusiastic to speak, the schools of contention, just Blossom
In fact, a lot of people in the interpretation of the reference like that PHP's underlying implementation of the matter, in fact, this is not right
Explain it with plain truth, not deep-seated principles. Otherwise, the more you talk, the more messy
Just like any C language book does not use assembly language to explain the pointer, because the upper levels do not understand how the bottom can understand
The PHP reference behaves as if it were a pointer. So it's no big deal to think of it as a "pointer."
It's just that you don't use references as "pointers" to arithmetic.
A different way to understand it is
The pointer is a house number for every store in the mall, so he can clearly know where the next one is.
The quote is in and out of the shop's signboard, so he doesn't know who's next.
In addition, the performance of the two is no different
Study it!
Here's what I hope you don't remember wrong:
The memory of PHP reference is +1 mechanism, that is, whenever one more reference counter +1,unset A-1
As long as the variable has a cell in use (not 0), the valid correlation variable name is still available
On this point of knowledge, I would like to say three points:
First : Only the variable can have the address, the value is not, if there is a variable in the function before adding &, must pass the variable;
$a =100; function Myfun (&a) {//& must be a variable, write directly 100 not, must assign 100 to the variable $ A, write $ A $a + +; return $a; } echo myfun ($a); 101, if written in Myfun (100), is wrong
Second: When using unset () Delete, only the reference relationship is deleted and no value is deleted
$a = "aaaaaaaa"; $b =& $a; Unset ($b);//unset ($b), delete only the reference relationship, and do not delete the value echo $a;//aaaaaaaa
Third: If you re-give a new reference to a variable, the reference relationship changes;
$a = "AAAAAAAAA"; $c = "CCCCCCCCC"; $b =& $a; $b =& $c;
I'll find you a picture on the Internet.
I'm asking this question, too.
I'm asking this question, too.
How do you understand that?