What does PHP mean by adding & in front of a variable?

Source: Internet
Author: User

Like what:
<? php

$a = ‘c‘ ;

$b = & $a ; //表示$b 和 $a 引用了同一个变量

$a = ‘abc‘ ; //这里重置了$a

echo $b ; //将输出abc

unset( $a ); //取消引用

echo $b ; //这里仍输出 abc

$a = ‘abcd‘ ;

echo $b ; //因为已经取消引用 这里仍输出abc

?> 

$a = & $b;


Read the 15th chapter of the Handbook:

What is a reference?
Referencing in PHP means accessing the same variable content 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 $a and $b point to the same variable.
Note: $a and $b are exactly the same here, not $a point to $b or vice versa, but $a and $b point to the same place.


The same syntax can be used in functions, it returns references, and is used in the new operator (PHP 4.0.4 and later versions):


<?php
$bar =& new Fooclass ();
$foo =& Find_var ($bar);
?>



Note: Using the & operator causes the object to generate a copy. If you use $this in a class, it will be used for the current instance of the class. No assignment with & will copy this instance (such as an object) and $this will act on the 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 messages in the constructor, such as @new, this does not work with the &new statement. 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:


<?php
function foo (& $var)
{
$var + +;
}

$a = 5;
Foo ($a);
?>


Will make the $a into 6. This is because the variable $var in the Foo function points to the same content that the $a points to. For more detailed explanations see reference delivery.

The third thing a reference does is to refer back.


Reference is not what
As mentioned earlier, references are not pointers. This means that the following structure does not produce the effect you expect:


<?php
function foo (& $var)
{
$var =& $GLOBALS ["Baz"];
}
Foo ($bar);
?>



This causes the $var variables in the Foo function to be bound together with the $bar when the function is called, but is then re-bound to the $GLOBALS ["Baz"]. It is not possible to bind $bar to other variables within the scope of a function call by means of a reference mechanism, because there is no variable $bar in the function foo (it is represented as a $var, but $var only the contents of the variable without calling the name-to-value binding in the symbol table).

Reference delivery
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:


<?php
function foo (& $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)

New statement, such as Foo (new Foobar ())

The reference returned from the function, for example:


<?php
function &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:


<?php
function bar ()//Note The Missing &
{
$a = 5;
return $a;
}
Foo (bar ());

Foo ($a = 5)//expression, not variable
Foo (5)//constant, not variable
?>


These conditions are PHP 4.0.4 and later versions.

Reference returns
The reference return 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:


<?php
function &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 = 2; statement), not a copy, as opposed to a reference syntax.

Note: Unlike parameter passing, the & symbol must be used in two places to indicate that a reference is returned instead of a normal copy, and that the $foo is the binding as a reference, not the usual assignment.

Dereference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:


<?php
$a = 1;
$b =& $a;
unset ($a);
?>


Not unset $b, just $a.

It might help to understand that this analogy with Unix's unlink.

Reference positioning
Many of the syntax structures of PHP are implemented by reference mechanisms, so all of the above reference bindings apply to these constructs as well. Some structures, such as reference passing and return, have already been mentioned above. Other structures that use references are:

Global references
When you declare a variable with the global $var, 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, that the unset $var does not unset global variables.

$this
In the method of an object, $this is always a reference to the object that called it.

Reprint: http://blog.csdn.net/remotesupport/article/details/5770618

What does PHP mean by adding & in front of a variable?

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.