PHP quote pass, reference return and dereference, and unset

Source: Internet
Author: User
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

  • Related Article

    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.