Php quote transfer, reference return, and unset

Source: Internet
Author: User
Php reference transfer, reference return and unreference and unset Original source: http://hi.baidu.com/kashifs/blog/item/928379f254888cbea50f524a.html? Referencing in PHP means that different names are used to access the same variable content. php references are passed, the reference returns, and the 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

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.