Reference in PHP, "&" explanation

Source: Internet
Author: User
Reference in PHP, "& amp;" explanation

  1. $ A = & $ B
  2. ?>

This means that $ a and $ B point to the same variable. Note: $ a and $ B are exactly the same here. this doesn't mean $ a points to $ B or vice versa, but $ a and $ B point to the same place. The same syntax can be used in functions, which return references, and in the new operator (PHP 4.0.4 and later versions ):

  1. $ Bar = & new fooclass ();
  2. $ Foo = & find_var ($ bar );
  3. ?>

Note: No & operator is used to generate a copy of the object. If you use $ this in the class, it will act on the current instance of the class. The instance (such as an object) will be copied without a value & and $ this will apply to this copy, which is not always the desired result. Because of performance and memory consumption problems, you usually only want to work on one instance.

Although you can use the @ operator to disable any error information in the constructor, for example, using @ new, it does not work when using the & new statement. This is a limitation of the Zend Engine and may cause a parsing error.

The second thing to do with referencing is to use referencing to pass variables. This is achieved by creating a local variable in the function and referencing the same content in the call range. For example:

  1. Function foo (& $ var)

  2. {
  3. $ Var ++;
  4. }

  5. $ A = 5;

  6. Foo ($ );
  7. ?>

Changes $ a to 6. This is because the variable $ var points to the same content as $ a in the foo function. For more detailed explanations, refer to transfer references. The third thing that references is to return a reference.

Reference is notAs mentioned above, the reference is not a pointer. This means that the following structure will not produce the expected results:

  1. Function foo (& $ var)
  2. {
  3. $ Var = & $ GLOBALS ["baz"];
  4. }
  5. Foo ($ bar );
  6. ?>

This binds the $ var variable in the foo function to $ bar when calling the function, but is then re-bound to $ GLOBALS ["baz. It is impossible to bind $ bar to another variable within the function call range through the reference mechanism, because there is no variable $ bar in function foo (it is represented as $ var, but $ var only contains the variable content and does not call the name-to-value binding in the symbol table ).

Reference transferYou can pass a variable to the function through reference, so that the function can modify the value of its parameter. Syntax:

  1. Function foo (& $ var)

  2. {
  3. $ Var ++;
  4. }

  5. $ A = 5;

  6. Foo ($ );
  7. // $ A is 6 here
  8. ?>

Note that there is no reference symbol when calling a function-only in the function definition. Function definition is enough to make parameters pass through references correctly.

The following content can be passed through reference: variable, such as foo ($)

New statement, such as foo (new foobar ())

Reference returned from the function, for example:

  1. Function & bar ()
  2. {
  3. $ A = 5;
  4. Return $;
  5. }
  6. Foo (bar ());
  7. ?>

For more information, see return references.

No other expressions can be passed through references, and the results are undefined. For example, the example passed by reference below is invalid:

  1. Function bar () // Note the missing &

  2. {
  3. $ A = 5;
  4. Return $;
  5. }
  6. Foo (bar ());

  7. Foo ($ a = 5) // expression, not a variable

  8. Foo (5) // constant, not a variable
  9. ?>

These conditions are available in PHP 4.0.4 and later versions.

Reference returnThe return value of a reference is used when you want to use the function to find the variable on which the reference should be bound. This syntax is used when a reference is returned:

  1. Function & find_var ($ param)

  2. {
  3. Return $ found_var;
  4. }

  5. $ Foo = & find_var ($ bar );

  6. $ Foo-> x = 2;
  7. ?>

In this example, the attributes of the objects returned by the find_var function will be set (translator: $ foo-> x = 2; statement), instead of copying, it is the same as not using the reference syntax.

Note: Unlike parameter transfer, the & symbol-must be used in both places to indicate that a reference is returned instead of a normal copy. it is also pointed out that $ foo is bound as a reference, instead of assigning values normally.

Cancel referenceWhen you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:

  1. $ A = 1;
  2. $ B = & $;
  3. Unset ($ );
  4. ?>

Not unset $ B, just $.

It may be helpful to understand this analogy with the Unix unlink call.

Reference and positioningMany PHP syntax structures are implemented through the reference mechanism, so all the above-mentioned reference binding applies to these structures. Some structures, such as reference transfer and return, have been mentioned above. Other referenced structures include:

Global ReferenceWhen a variable is declared with global $ var, a reference to the global variable is actually created. That is to say, it is the same as doing so:

  1. $ Var = & $ GLOBALS ["var"];
  2. ?>

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

$ This is always a reference to the object that calls an object in the method of an object. Articles you may be interested in: php reference the detailed introduction of passing a value through the instance to understand the difference between passing a value in php and transferring a reference through the instance to see the efficiency of php address reference problems related to changing the variable value of the php reference address

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.