PHP reference passing, referencing return and dereference, and unset
Original source: http://hi.baidu.com/kashifs/blog/item/928379f254888cbea50f524a.html
?
Referencing in PHP means accessing the same variable content with a different name. This is not like a pointer to C , instead of a reference is a symbol table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names.
?
?
Reference delivery
function foo (& $var) {$var + +;} Foo ($a); Note that there is no reference symbol in the function call-only in the function definition. The function definition alone is enough to make the argument pass through the reference correctly.
?
Reference returns
function &init_users () {... return $cls;} Return using a reference, must be in two places with & symbol $user = & init_users (); function &init_users () {... return $cls;}
?
Dereference
When unset a reference, only the binding between the variable name and the variable content is broken. This does not mean that the contents of the variable are destroyed.
$a = "Hihaha"; $b = & $a; unset ($b); echo$a;//shows "Hihaha"
?
----------------------------------------------------------------
Here is an example of a PHP reference, and take a good look:
?
----------------------------------------------------------------
PHP unset Global Variables can only destroy local variables in user functions, and cannot destroy global variables . (Starting with PHP4 unset is no longer a function, but a statement ). What if the global variables need to be destroyed? It is also very simple, using the $globals array to implement.
<? PHP function foo () {unset ($GLOBALS [' Bar ']); Instead of unset ($bar)} $bar = "something"; Foo (); Var_dump ($bar);?>
?
For unset:
1. The function frees memory only if the value of the variable takes up more than 256 bytes of space
2. The address is freed only if all variables that point to the value (for example, a reference variable points to that value) are destroyed (and a 1 judgment is performed)
That is, check for other variable bindings, and some will not be released. Just like this example:
$a = "Hihaha"; $b = & $a; unset ($b); echo$a;//shows "Hihaha"
?
So we recommend that you use the $ variable =null method to release its memory.
? Give a function that tests the memory usage of the current PHP script:
?
I still can't understand, look here: http://www.laruence.com/2011/03/04/1894.html