Explanations of PHP references

Source: Internet
Author: User

What is a reference?

Referencing in PHP means accessing the same variable content with a different name. This is not like the C pointer, instead, the reference is the symbol table alias. 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 hardlink in Unix file systems.

Reference to do what

PHP references allow 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.

Note:

If an array with a reference is copied, its value is not dereferenced. This is true for arrays that pass values to functions.

Note:

If a reference is assigned to an undefined variable, a reference parameter is passed, or a reference is returned, the variable is created automatically.

Example #1 use references to undefined variables

<?php    function foo (& $var) {}    foo ($a),//$a is ' created ' and assigned to null    $b = Array ();    Foo ($b [' B ']);    Var_dump (array_key_exists (' B ', $b)); BOOL (true)    $c = new StdClass;    Foo ($c->d);    Var_dump (property_exists ($c, ' d ')); BOOL (TRUE)?>

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);? >

Since PHP 5, new automatically returns references, so using =& here is obsolete and generates E_STRICT-level messages.

Note:

Using the & operator causes the object to generate a copy. If $this in the 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, you usually just want to work on one instance.

Although you can use the @ operator to suppress any error information 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.

warning If a variable declared as global is assigned to a reference within a function, the reference is only visible inside the function. You can avoid this by using an array of $GLOBALS.

Example #2 referencing global variables within a function

<?php    $var 1 = "Example variable";    $var 2 = "";    function global_references ($use _globals)    {        Global $var 1, $var 2;        if (! $use _globals) {            $var 2 =& $var 1;//visible only inside the function        } else {            $GLOBALS ["Var2"] =& $var 1; Visible also in global context        }    }    global_references (false);    echo "VAR2 is set to ' $var 2 ' \ n"; VAR2 is set to '    global_references (true);    echo "VAR2 is set to ' $var 2 ' \ n"; VAR2 is set to ' Example variable '?>

$var the global; As $var =& $GLOBALS [' var ']; The shorthand. Thus assigning other references to $var only changes the reference to the local variable.

Note:

If you assign a value to a variable that has a reference in the foreach statement, the referenced object is also changed.

Example #3 References and foreach statements

<?php    $ref = 0;    $row =& $ref;    foreach (Array (1, 2, 3) as $row) {        //do something    }    echo $ref;//3-last element of the iterated array?>

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.

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 expected effect:

<?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). You can use a reference return to refer to the variable selected by the function.

  • 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.