True understanding of Reference & amp; in php

Source: Internet
Author: User
Php references (that is, adding symbols before variables, functions, and objects) are most important to deleting referenced variables, but the referenced variables cannot be accessed, but the content is not destroyed. Reference in PHP means that different names access the same variable content. variable reference PHP allows you to use two variables to point to the same

Php reference (that is, adding a symbol before a variable, function, or object) // The most important thing is to delete the referenced variable, but the referenced variable cannot be accessed, but the content is not destroyed. Reference in PHP means that different names access the same variable content. variable reference PHP allows you to use two variables to point to the same

PhpReference(That is, add the & symbol before variables, functions, and objects) // Delete the most important part.ReferenceOnlyReferenceBut the content is not destroyed in PHP.ReferenceIt means that different names access the content of the same variable.

VariableReference
PHPReferenceYou can use two variables to point to the same content.

 

I will not say much about the function's address transfer call. The following code is provided directly.

 "; Echo $ B; // output 101?>


It should be noted that test (1); will cause an error because it is transmitted as required by PHP.ReferenceIt cannot be a constant (you can see the error prompt ).

FunctionReferenceReturnFirst look at the code

 


This method $ a = test (); is not actually a function.ReferenceReturns, which is no different from common function calls. The reason is that PHP requires that the function be obtained through the $ a = & test (); method.ReferenceWhat is returned?ReferenceReturn (PHP manual says:ReferenceThe return value is used when you want to use the function to findReference.) I haven't understood this sentence for a long time.

In the example above, $ a = test () is used to call a function. It only assigns the value of the function to $ a, and $ a makes any changes, will not affect $ B in the function,But how to call a function through $ a = & test, his role is to direct the memory address of the $ B Variable in return $ B to the memory address of the $ a variable to the same place, which produces the equivalent effect ($ a = & B ;) so change the value of $.The value of $ B is also changed. Therefore, after $ a = & test (); $ a = 5; is executed, the value of $ B is changed to 5.

This is to make everyoneUnderstandingFunctionReferenceStatic variables are used only when the return result is returned.ReferenceThe returned results are mostly used in objects.

ObjectReference

 Abc; // here ABC echo $ c-> abc; // here ABC $ B-> abc = "DEF"; echo $ c-> abc; // output DEF here?>

The above code is the running effect in PHP5In PHP5, the object is copied throughReferenceTo achieve. In the above column, $ B = new a; $ c = $ B; in fact, it is equivalent to $ B = new a; $ c = & $ B; PHP5 usesReferenceObject, but sometimes you may want to create a copy of the object, and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone.

ReferenceFunction
If the program is large,ReferenceThere are many variables for the same object, and you want to manually clear the object after it is used up. We recommend that you use the "&" method and then use the $ var = null method to clear it. in other cases, use the default php5 method. in addition, we recommend that you use the "&" Method for transferring large arrays in php5 to save memory space.

CancelReferenceWhen you unsetReference, But breaks the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:

Not unset $ B, just $. Function quoteTest () {global $ var; // equivalent to $ var = & $ GLOBALS ['var']; unset ($ var); // Delete is just deleteReference, AndReferenceThe content still exists. The same as above does not mean that the variable content is destroyed} $ var = 1; quoteTest (); echo $ var; // result 1

Bytes ----------------------------------------------------------------------------------------------

Not unset $ B, just $.

Function quoteTest () {global $ var; // equivalent to $ var = & $ GLOBALS ['var']; $ var = 5; // because they all point to the same memory content} $ var = 1; quoteTest (); echo $ var; // result 5 callback ----------------------------------------------------------------------------------------------

'&':Reference

GlobalReferenceWhen a global $ var variable is used to declare a variable, a global variable is actually created.Reference. That is to say, it is the same as doing so:

This means that, for example, unset $ var does not unset global variables.

$ This is in the method of an object. $ this is always the object that calls it.Reference.

// The following is an episode. in php, the address pointing (similar to pointer) function is not implemented by the user, but by the Zend core. in phpReferenceThe "Copy at write" principle is used, that is, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

1. If the following code [php] $ a = "ABC"; $ B = $; [/php] In fact, both $ a and $ B point to the same memory address, not that $ a and $ B occupy different memory.

2: Add the following code [php] $ a = "EFG" based on the above Code "; [/php] Because $ a and $ B point to the memory data to be re-written, the Zend core automatically determines that $ B will generate a $ a data copy, apply for a new memory for storage

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.