Php reference transfer, reference return, and unset Source: hi.baidu.comkashifsblogitem928424f254888cbea50f524a.html? Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note: In PHP, the variable name and variable content are php reference transfer, reference return and unreference, and unset
Source: http://hi.baidu.com/kashifs/blog/item/928379f254888cbea50f524a.html
?
Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names.
?
?
Reference transfer
Function foo (& $ var) {$ var ++;} foo ($ a); // note that no reference symbol is used during function calling-only in function definition. Function definition is enough to make parameters pass through references correctly.
?
Reference return
Function & init_users (){... return $ cls;} // return with reference. The & symbol $ user = & init_users (); function & init_users () {... must be used in both places (){... return $ cls ;}
?
Cancel reference
When unset is a reference, it only disconnects the binding between the variable name and the variable content. This does not mean that the variable content is destroyed.
$a="hihaha";$b= &$a;unset($b);echo$a;// shows "hihaha"
?
----------------------------------------------------------------
Here is an example referenced by php:
?
----------------------------------------------------------------
PHP unset global variables can only destroy local variables in user functions, and cannot destroy global variables. (Since PHP4, unset is no longer a function, but a statement ). What should I do if I want to destroy global variables? It is also very simple. it is implemented using the $ GLOBALS array.
<? PHP function foo () {unset ($ GLOBALS ['bar']); // instead of unset ($ bar)} $ bar = "something"; foo (); var_dump ($ bar);?>
?
For unset:
1. this function only releases memory when the space occupied by variable values exceeds 256 bytes.
2. the address will be released only when all the variables pointing to this value (for example, a referenced variable pointing to this value) are destroyed (the judgment of 1 is also required)
That is, check whether there are other variable bindings, and if there is one, it will not be released. Like this example:
$a="hihaha";$b= &$a;unset($b);echo$a;// shows "hihaha"
?
Therefore, we recommend that you use the $ variable = null method to release the memory.
? To test the memory usage of the current php script:
?
I still don't understand, see here: http://www.laruence.com/2011/03/04/1894.html