What is the meaning and function of the & symbol before the PHP function name?

Source: Internet
Author: User
The & notation in PHP is a reference to a variable instead of a copy, which 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.

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.

What is the meaning of the & symbol in front of the PHP function? Let's start with two demo code and then explain.

function &chhua () {Static $b = "www.php.cn";//declares a static variable $b= $b. " Web development "; Echo $b; return $b;}  $a =chhua ();//This statement outputs $b value for "www.php.cnWEB development" $a = "PHP"; echo "<Br>"; $a =chhua ();//This statement outputs $b value of " Www.php.cnWEB Developing Web Development "echo" <Br> "$a =&chhua ();//This statement outputs $b value of" Www.php.cnWEB Developing Web Development "echo" <Br> "; $a =" JS "; $a =chhua (); This statement outputs the value of the $b "Jsweb development"    function &test () {    static $b =0;//declares a static variable    $b = $b +1;    echo $b;    return $b;}  $a =test ();//This statement will output $b value of 1$a=5, $a =test ();//This statement will output a value of $b 2$a=&test ();//This statement will output $b value 3$a=5; $a =test (); This statement will output a value of 6 for the $b

Let's explain the second function.
In this way $a=test () is not actually returned by a function reference, which is not the same as a normal function call.

As for the reason: this is the PHP rule
PHP rules through $a=&test (); The way to get is to return the reference to the function.

As for what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) )

Using the above example to explain is
$a =test () call the function, just assign the value of the function to $ A, and no change to $ A will not affect the $b in the function.
instead of calling the function by $a=&test (), his role is to return The memory address of the $b variable in the $b and the memory address of the $ A variable,
points to the same place. That produces the equivalent effect ($a =&b;) So change the value of $ A and change the value of $b at the same time, so it executes:
$a =&test (); $a = 5; After that, $b value becomes 5.

Related Article

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.