PHP Reference =& Detailed

Source: Internet
Author: User
Tags what php

reference what is referenced in PHP means that the content of the same variable is accessed with a different name. This is not like a pointer to C, they are symbol table aliases. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix filename and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be seen as tight connections in Unix file systems. Reference to do what PHP references allow you to use two variables to point to the same content. This means that when you do this:<?PHP$a=&$b?>This means that$aAnd$bpoints to the same variable. Note:$aAnd$bHere is exactly the same and that's not$aPoints to the$bOr vice versa, but$aAnd$bpoint to the same place. The same syntax can be used in functions, it returns references, and is used inNewOperator (PHP 4.0.4and later versions):<?PHP$bar=&NewFooclass ();$foo=& Find_var ($bar);?>Note: Using the & operator causes the object to generate a copy. If you are in class$this, which will be used for the current instance of the class. No assignment with & will copy this instance (such as an object) and$thiswill act on this copy, which is not always the desired result. Because of problems with performance and memory consumption, usually you just want to work on one instance. Although you can use the @ operator to close any error message in the constructor, such as @New, but with &Newstatement, this is not effective. This is a limitation of the Zend engine and can result in a parsing error. The second thing a reference does is pass a variable by reference. This is done by creating a local variable within the function and referencing the same content within the call range. For example:<?PHPfunctionFoo (&$var){    $var++;}$a=5; Foo ($a);?>will make$abecomes 6. This is because the variables in the Foo function$varpointed to the and$athe same content that points to. For more detailed explanations see reference delivery. The third thing a reference does is to refer back. A reference is not a pointer, as mentioned earlier. This means that the following structure does not produce the effect you expect:<?PHPfunctionFoo (&$var){    $var=&$GLOBALS["Baz"];} Foo ($bar);?>This will make the Foo function$varVariables at function calls and$barare bound together, but are then re-bound to the$GLOBALS["Baz"] above. It is not possible to use the reference mechanism$barBind to another variable within the scope of the function call because there is no variable in the function foo$bar(It is represented as$varBut$varonly the contents of the variable are not called by the name-to-value binding in the symbol table. Reference passing you can pass a variable to a function by reference, so that the function can modify the value of its argument. The syntax is as follows:<?PHPfunctionFoo (&$var){    $var++;}$a=5; Foo ($a);//$a is 6 here?>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. The following can be passed by reference: variables, such as foo ($a) NewStatements, such as Foo (Newfoobar ()) returns a reference from a function, for example:<?PHPfunction&Bar () {$a= 5; return $a;} Foo (bar ());?>See the reference return for a detailed explanation. No other expression can be passed by reference, and the result is undefined. For example, the following example of a reference pass is invalid:<?PHPfunctionBar ()//Note the Missing &{    $a= 5; return $a;} Foo (bar ()); Foo ($a= 5)//an expression, not a variableFoo (5)//constants, not variables?>These conditions are PHP4.0.4and later versions. The reference return reference is used when you want to use a function to find out which variable the reference should be bound to. Use this syntax when you return a reference:<?PHPfunction&find_var ($param){    /*.... Code ...*/    return $found _var;}$foo=& Find_var ($bar);$foo->x = 2;?>in this example, the properties of the object returned by the Find_var function are set (translator: refers to$foo->x = 2statements), not copies, are not the same as reference syntax. Note: Unlike parameter passing, the & symbol must be used in two places to indicate that a reference is returned instead of a regular copy, which also indicates$foois the binding as a reference, not the usual assignment. Dereference When youunseta reference that simply breaks the binding between the variable name and the content of the variable. This does not mean that the contents of the variable are destroyed. For example:<?PHP$a= 1;$b=&$a;unset($a);?>won'tunset $bIt's just that$a. Take this and Unix.unlinkThe analogy may help to understand. Reference positioning many PHP syntax structures are implemented by reference mechanisms, so all of the above reference bindings apply to these structures as well. Some structures, such as reference passing and return, have already been mentioned above. Other structures that use references are:Globalreference when usingGlobal $varwhen you declare a variable, you actually establish a reference to the global variable. In other words, it is the same as doing this:<?PHP$var=&$GLOBALS["Var"];?>This means, for example, thatunset $varNounsetGlobal variables. $thisin the method of an object,$thisis always a reference to the object that called it. <?PHP/** * (C) CopyRight magictoolbox-www.magictoolbox.com-[email protected]**/require_once(Dir_fs_catalog. Dir_ws_modules. ' Magictoolbox/magictoolbox_addons.php ');$mod= Magictoolboxloadmodule (' Magiczoomplus ');$enabled=false;$main _page=isset($_request[' Main_Page ']) ?$_request[' Main_Page ']:$_get[' Main_Page '];$cPath=isset($_request[' CPath ']) ?$_request[' CPath ']:$_get[' CPath '];$products _id=isset($_request[' products_id ']) ?$_request[' products_id ']:$_get[' products_id '];$scroll=false;if($mod->type = = ' Standard ' &&!$mod->params->checkvalue (' template ', ' original ') &&$mod->params->checkvalue (' Magicscroll ', ' yes ')) {    $scroll= Magictoolboxloadmodule (' Magicscroll '); $scroll->params->appendarray ($mod->params->GetArray ());$scroll->params->set (' direction ',$mod->params->checkvalue (' template ',Array(' Left ', ' right ') ? ' Bottom ': ' Right ');}if(Zen_magiczoomplus_status = = ' true ') {    $enable _on_block=  !$mod->params->checkvalue (' Use-effect-on-whats-new-block ', ' No ') | | !$mod->params->checkvalue (' Use-effect-on-specials-block ', ' No ') | | !$mod->params->checkvalue (' Use-effect-on-featured-block ', ' No '); if($mod->type = = ' Standard ') {        if( (intval($cPath) > 0 && (!$mod->params->checkvalue (' use-effect-on-category-page ', ' No ') | |$enable _on_block)) ||//category page(intval($products _id) > 0 && (!$mod->params->checkvalue (' use-effect-on-product-page ', ' No ') | |$enable _on_block)) ||//product Page($main _page= = ' Products_new ' && (!$mod->params->checkvalue (' use-effect-on-products-new-page ', ' No ') | |$enable _on_block)) ||            ($main _page= = ' Products_all ' && (!$mod->params->checkvalue (' use-effect-on-products-all-page ', ' No ') | |$enable _on_block)) ||            ($main _page= = ' Advanced_search_result ' && (!$mod->params->checkvalue (' use-effect-on-products-search-page ', ' No ') | |$enable _on_block))        ) {            $enabled=true; }    } ElseIf($mod->type = = ' Category ' | |$mod->type = = ' Circle ') {        if( (intval($products _id) > 0 && (!$mod->params->checkvalue (' use-effect-on-product-page ', ' No ') | |$enable _on_block)) ||//product Page(intval($cPath) > 0 &&$enable _on_block) ||//category page($main _page= = ' Products_new ' &&$enable _on_block) ||            ($main _page= = ' Products_all ' &&$enable _on_block) ||            ($main _page= = ' Advanced_search_result ' &&$enable _on_block)        ) {            $enabled=true; }    }}if(!$GLOBALS["magiczoomplus_headers_loaded"] &&$enabled) {    Echo $mod->headers (Dir_ws_catalog. Dir_ws_template. ' JScript ', Dir_ws_catalog. Dir_ws_template. ' CSS '); if($scroll) {        Echo $scroll->headers (Dir_ws_catalog. Dir_ws_template. ' JScript ', Dir_ws_catalog. Dir_ws_template. ' CSS '); }    $GLOBALS["magiczoomplus_headers_loaded"] =true;}

Original address: http://www.cnblogs.com/awinlei/archive/2013/03/13/2957058.html

PHP Reference =& detailed

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.