PHP variable reference puzzles I thought I was already very clear about the variables reference and assignment. The result encountered the following problem, which completely overturned my understanding & lt ;? Php & nbsp; $ str & nbsp; = & nbsp; 'wangchuanbo'; & nbsp; & PHP variable reference doubts
I thought that I had already made it clear that I had referenced and assigned a value to the variable. as a result, I encountered the following problem, which completely overturned my understanding.
$str = 'WangChuanbo';
$s = &$str;
unset($str);
echo $s,'hello world';
?>
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 ??
Php variable reference
------ 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.
------ Solution --------------------
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.
------ Solution --------------------
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.
------ Solution --------------------
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!
------ Solution --------------------
Top Post favorites ~~
------ Solution --------------------
Reference:
Reference: unset is a reference. it only disconnects the binding between the variable name and the variable content.
$ S = & $ 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.
------ Solution --------------------
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
------ Solution --------------------
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.
------ Solution --------------------